pyinstaller打包遇到的問題解決
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)
一、說明
關(guān)于Pyinstaller,可以查看Python打包發(fā)布1(基于Pyinstaller打包多目錄項(xiàng)目)
Pyinstaller可以將資源文件一起打包到exe中,當(dāng)exe在運(yùn)行時(shí),會(huì)生成一個(gè)臨時(shí)文件夾,程序可通過sys._MEIPASS訪問臨時(shí)文件夾中的資源。
程序打包成exe的時(shí),會(huì)將一個(gè)變量frozen注入到sys中,這里使用判斷,檢測(cè)sys是否有frozen這個(gè)變量,如果有,就使用sys._MEIPASS訪問臨時(shí)文件夾路徑,如果沒有,就使用當(dāng)前路徑,當(dāng)程序不管是以PyInstaller打包后形式運(yùn)行,還是本地測(cè)試,都能找到正確的資源路徑。
二、測(cè)試
本項(xiàng)目,目錄為:
- 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)
未打包時(shí)運(yùn)行,打印結(jié)果分別為:
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打包后,打印結(jié)果分別為:
['_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
此時(shí)路徑為app.exe
\APP\dist\app.exe
三、解決思路
根據(jù)自己目錄的相對(duì)位置進(jìn)行修改
pyinstaller打包后運(yùn)行時(shí),輸出sys.executable的路徑。py運(yùn)行時(shí)輸出__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目錄
自行調(diào)整項(xiàng)目路徑
pj_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.abspath(app_path())))) print(f"================Project dir is '{pj_path}'===============")
到此這篇關(guān)于pyinstaller打包遇到的問題解決的文章就介紹到這了,更多相關(guān)pyinstaller打包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過實(shí)例了解python__slots__使用方法
這篇文章主要介紹了通過實(shí)例了解python__slots__使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Selenium向iframe富文本框輸入內(nèi)容過程圖解
這篇文章主要介紹了Selenium向iframe富文本框輸入內(nèi)容過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04對(duì)python當(dāng)中不在本路徑的py文件的引用詳解
今天小編就為大家分享一篇對(duì)python當(dāng)中不在本路徑的py文件的引用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12Opencv實(shí)現(xiàn)眼睛控制鼠標(biāo)的實(shí)踐
本文主要介紹了Opencv實(shí)現(xiàn)眼睛控制鼠標(biāo)的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02針對(duì)Pandas的總結(jié)以及數(shù)據(jù)讀取_pd.read_csv()的使用詳解
這篇文章主要針對(duì)Pandas總結(jié)以及數(shù)據(jù)讀取_pd.read_csv()的使用詳解做出了實(shí)例,講解非常全面,值得收藏,需要的朋友可以參考下2023-03-03