pyinstaller打包遇到的问题解决

作者:一世繁华行 时间:2021-07-22 07:56:24 

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

pyinstaller打包遇到的问题解决

解决办法:–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’

pyinstaller打包遇到的问题解决

解决办法:
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

标签:pyinstaller,打包
0
投稿

猜你喜欢

  • python实现手机通讯录搜索功能

    2023-11-04 08:04:08
  • CSS文件的编码要和页面的编码相一致

    2010-06-06 13:59:00
  • python人工智能tensorflow常见损失函数LOSS汇总

    2023-11-22 18:12:48
  • Oracle 数据库操作技巧集

    2010-07-26 12:49:00
  • 如何防止未经注册的用户绕过注册界面直接进入应用系统?

    2009-11-22 19:22:00
  • Pycharm配置autopep8实现流程解析

    2021-08-23 05:34:40
  • JSP安全开发之XSS漏洞详解

    2023-06-13 13:07:24
  • Mootools 1.2教程(10)——Fx.Tween的使用

    2008-12-02 18:03:00
  • 数据库疑难讲解:改善SQL Server内存管理

    2009-10-29 13:30:00
  • 网页设计详细教程之XML简便省力技巧五则

    2008-05-23 14:37:00
  • go语言实现markdown解析库的方法示例

    2023-06-20 07:37:32
  • python GUI编程(Tkinter) 创建子窗口及在窗口上用图片绘图实例

    2021-12-21 19:37:29
  • windows上安装python3教程以及环境变量配置详解

    2023-11-11 08:09:35
  • 解决新版Pycharm中Matplotlib图像不在弹出独立的显示窗口问题

    2023-06-28 02:44:14
  • 浅谈Python NLP入门教程

    2021-04-18 14:45:53
  • python 下载文件的几种方式分享

    2021-03-27 14:08:17
  • Python实现Word文档转换Markdown的示例

    2022-06-24 08:23:29
  • 如何巧妙利用SQL Server的EXISTS结构

    2009-02-19 17:36:00
  • 在SQL Server 2005数据库中更改数据架构

    2009-01-19 13:06:00
  • MySQL数据库临时文件究竟储存在哪里

    2009-09-06 12:11:00
  • asp之家 网络编程 m.aspxhome.com