日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

pyinstaller打包遇到的問題解決_python

作者:一世繁華行 ? 更新時間: 2023-05-16 編程語言

1、ModuleNotFoundError: No module named ‘scipy.spatial.transform_rotaion_groups’

解決辦法:–hidden-import scipy.spatial.transform._rotation_groups

2、FileNotFoundError:[Errno 2] No such file or directory:‘C:\Users\Gw0021\AppData\Local\Temp\_MEI149922\matplotlib\npl-data\matplotlibrc’

解決辦法:
pip uninstall matplotlib
pip install matplotlib==3.1.3

3、Failed to execute script ‘app‘ due to unhandled exception已解決

參考解決方法:[Failed to execute script ‘app‘ due to unhandled exception](https://blog.csdn.net/qq_25262697/article/details/127991930?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2~default~OPENSEARCH~Rate-2-127991930-blog-122015848.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~OPENSEARCH~Rate-2-127991930-blog-122015848.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=3)

一、說明

關于Pyinstaller,可以查看Python打包發布1(基于Pyinstaller打包多目錄項目)

Pyinstaller可以將資源文件一起打包到exe中,當exe在運行時,會生成一個臨時文件夾,程序可通過sys._MEIPASS訪問臨時文件夾中的資源。
程序打包成exe的時,會將一個變量frozen注入到sys中,這里使用判斷,檢測sys是否有frozen這個變量,如果有,就使用sys._MEIPASS訪問臨時文件夾路徑,如果沒有,就使用當前路徑,當程序不管是以PyInstaller打包后形式運行,還是本地測試,都能找到正確的資源路徑。

二、測試

本項目,目錄為:

- MyProject
	- In
	- Out
	- App
		- __init__.py
		- app.py
		- MainProgram
			- __init__.py
			- 1.py
			- 2.py
			- main.py

可以修改main.py,添加

import os,sys
print(sys)
print(sys.executable)

未打包時運行,打印結果分別為:
sys加粗樣式

['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '__unraisablehook__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_framework', '_getframe', '_git', '_home', '_xoptions', 'addaudithook', 'api_version', 'argv', 'audit', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'pycache_prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'unraisablehook', 'version', 'version_info', 'warnoptions', 'winver']

sys.executable

C:\ProgramData\Anaconda3\envs\nuitka\python.exe

pyinstaller打包后,打印結果分別為:

['_MEIPASS', '__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '__unraisablehook__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_framework', '_getframe', '_git', '_xoptions', 'addaudithook', 'api_version', 'argv', 'audit', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'frozen', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'getallocatedblocks', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'platlibdir', 'prefix', 'pycache_prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'unraisablehook', 'version', 'version_info', 'warnoptions', 'winver']

可以看到多了’_MEIPASS’,‘frozen’
sys.executable
此時路徑為app.exe

\APP\dist\app.exe

三、解決思路

根據自己目錄的相對位置進行修改
pyinstaller打包后運行時,輸出sys.executable的路徑。py運行時輸出__file__

# pyinstaller
def app_path():
    # Returns the base application path.
    if hasattr(sys, 'frozen'):
        # Handles PyInstaller
        return sys.executable  #使用pyinstaller打包后的exe目錄
    return os.path.dirname(__file__)     #沒打包前的py目錄

自行調整項目路徑

pj_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.abspath(app_path()))))
print(f"================Project dir is '{pj_path}'===============")

原文鏈接:https://blog.csdn.net/jianglianye21/article/details/120307712

欄目分類
最近更新