網站首頁 編程語言 正文
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
相關推薦
- 2022-03-27 Docker下安裝Mongo4.2及客戶端工具連接Mongo_docker
- 2022-05-01 Python?數據可視化神器Pyecharts繪制圖像練習_python
- 2022-06-01 Android調用外置攝像頭的方法_Android
- 2022-03-09 C++實現模擬shell命令行(代碼解析)_C 語言
- 2023-07-27 axios介紹以及對axios進行二次封裝
- 2022-06-30 Python中隱藏的五種實用技巧分享_python
- 2024-01-14 Spring boot 注解@Async不生效 無效 不起作用
- 2022-07-01 python神經網絡Densenet模型復現詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支