pyinstaller打包遇到的问题解决

 更新时间:2023年02月17日 08:30:51   作者:一世繁华行  
本文主要介绍了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)

一、说明

关于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}'===============")

到此这篇关于pyinstaller打包遇到的问题解决的文章就介绍到这了,更多相关pyinstaller打包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用PySide多线程处理图形界面卡顿问题详解

    使用PySide多线程处理图形界面卡顿问题详解

    这篇文章主要介绍了使用PySide多线程处理图形界面卡顿问题,在制作图形界面时,只用一个线程很容易导致卡顿无响应,一旦主线程被阻塞,那么整个图形界面都会无法继续使用,为了解决这个问题,就得使用多线程,需要的朋友可以参考下
    2025-04-04
  • Pycharm新手使用教程(图文详解)

    Pycharm新手使用教程(图文详解)

    这篇文章主要介绍了Pycharm新手使用教程(图文详解),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Python实现求解一元二次方程的方法示例

    Python实现求解一元二次方程的方法示例

    这篇文章主要介绍了Python实现求解一元二次方程的方法,涉及Python基于math包进行数值运算相关操作技巧,需要的朋友可以参考下
    2018-06-06
  • Python 创建TCP服务器的方法

    Python 创建TCP服务器的方法

    这篇文章主要介绍了Python 创建TCP服务器的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • python实现三阶魔方还原的示例代码

    python实现三阶魔方还原的示例代码

    这篇文章主要介绍了python实现三阶魔方还原的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 利用PyCharm Profile分析异步爬虫效率详解

    利用PyCharm Profile分析异步爬虫效率详解

    这篇文章主要给大家介绍了关于如何利用PyCharm Profile分析异步爬虫效率的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用PyCharm具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • 利用Python批量循环读取Excel的技巧分享

    利用Python批量循环读取Excel的技巧分享

    这篇文章主要为大家详细介绍了何用Python批量循环读取Excel,文中的示例代码讲解详细,对我们的学习或工作有一定的帮助,感兴趣的可以了解一下
    2023-07-07
  • Python中FastAPI项目使用 Annotated的参数设计的处理方案

    Python中FastAPI项目使用 Annotated的参数设计的处理方案

    FastAPI 是一个非常现代化和高效的框架,非常适合用于构建高性能的 API,FastAPI 是一个用于构建 API 的现代、快速(高性能)web 框架,基于 Python 类型提示,这篇文章主要介绍了Python中FastAPI项目使用 Annotated的参数设计,需要的朋友可以参考下
    2024-08-08
  • Python面向对象的内置方法梳理讲解

    Python面向对象的内置方法梳理讲解

    面向对象编程是一种编程方式,此编程方式的落地需要使用“类”和 “对象”来实现,所以,面向对象编程其实就是对 “类”和“对象” 的使用,今天给大家介绍下python 面向对象开发及基本特征,感兴趣的朋友一起看看吧
    2022-10-10
  • python 中的列表生成式、生成器表达式、模块导入

    python 中的列表生成式、生成器表达式、模块导入

    这篇文章主要介绍了python中的列表生成式、生成器表达式、模块导入 ,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-06-06

最新评论