Pyinstaller 可执行文件打开并立即关闭
Posted
技术标签:
【中文标题】Pyinstaller 可执行文件打开并立即关闭【英文标题】:Pyinstaller executable opens and immediately closes 【发布时间】:2021-11-02 14:34:46 【问题描述】:我正在尝试使用 pyinstaller 从 python 项目创建可执行文件。但是,当我运行可执行文件时,它会立即打开和关闭。 从命令行运行 pyinstaller 时,我没有收到一个错误和两个警告。运行可执行文件时没有错误消息。
(但我不认为这两个警告应该有问题,因为我已经创建了具有相同警告的其他可执行文件并且它们仍然可以正常工作!)
当我跑步时
pyinstaller --debug=all main.py
我明白了
12070 WARNING: Hidden import "pkg_resources.py2_warn" not found!
12070 WARNING: Hidden import "pkg_resources.markers" not found!
...
Traceback (most recent call last):
File "[path]/venv/bin/pyinstaller", line 10, in <module>
sys.exit(run())
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/__main__.py", line 126, in run
run_build(pyi_config, spec_file, **vars(args))
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/__main__.py", line 65, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/building/build_main.py", line 815, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/building/build_main.py", line 762, in build
exec(code, spec_namespace)
File "[path]/main.spec", line 37, in <module>
coll = COLLECT(exe,
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/building/api.py", line 855, in __init__
self.__postinit__()
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/building/datastruct.py", line 159, in __postinit__
self.assemble()
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/building/api.py", line 883, in assemble
os.makedirs(todir)
File "/usr/local/Cellar/python@3.9/3.9.1_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 225, in makedirs
mkdir(name, mode)
NotADirectoryError: [Errno 20] Not a directory: '[path]/dist/main/main/GUI'
当我现在跑步时
pyinstaller --hidden-import pkg_resources.py2_warn --hidden-import pkg_resources.markers --debug=all main.py
我明白了
11705 ERROR: Hidden import 'pkg_resources.py2_warn' not found
11705 ERROR: Hidden import 'pkg_resources.markers' not found
...
Traceback (most recent call last):
File "[path]/venv/bin/pyinstaller", line 10, in <module>
sys.exit(run())
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/__main__.py", line 126, in run
run_build(pyi_config, spec_file, **vars(args))
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/__main__.py", line 65, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/building/build_main.py", line 815, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/building/build_main.py", line 762, in build
exec(code, spec_namespace)
File "[path]/main.spec", line 37, in <module>
coll = COLLECT(exe,
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/building/api.py", line 855, in __init__
self.__postinit__()
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/building/datastruct.py", line 159, in __postinit__
self.assemble()
File "[path]/venv/lib/python3.9/site-packages/PyInstaller/building/api.py", line 883, in assemble
os.makedirs(todir)
File "/usr/local/Cellar/python@3.9/3.9.1_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 225, in makedirs
mkdir(name, mode)
NotADirectoryError: [Errno 20] Not a directory: '[path]/dist/main/main/GUI'
我已经被这个问题困扰了好几天了,现在我真的不知道该怎么办!
我的系统规格:
MacOS 11.5.2 Python 3.9 pyinstaller 4.5.1(Python 包) setuptools 57.5.0(Python 包) PyQt5 5.15.4(Python 包) pyqtgraph 0.12.2(Python 包)我在虚拟环境中运行一切。
我的项目结构是:
Project
|- dist
|- build
|- main.spec
|
|- main.py
|- main
| |- GUI
| |- MainWindow.py
| |- ui.py
| |- Sim
| |- Setter.py
| |- Integrator.py
【问题讨论】:
您可以使用--debug=all
在运行时获取更多错误消息(在可执行文件上)
@gfdsweds 我已经尝试过了,现在确实收到一条错误消息:`NotADirectoryError: [Errno 20] Not a directory: './dist/main/main/GUI' ` where @ 987654328@ 是我项目中的一个文件夹。
我现在会用新的错误更新我的帖子
它将错误指向的代码行是什么?
@Joe 我已经在帖子中添加了整个回溯
【参考方案1】:
在我将目录名称 main
从 main
更改为 MainPackage
后,可执行文件现在可以工作了。
我认为错误在于我的入口点main.py
与目录main
同名(其中包含被调用的其他代码)。所以 pyinstaller 把目录和入口文件混淆了。
另外,我不必包含两个隐藏的导入来使可执行文件工作(如怀疑的那样)。构建可执行文件的命令很简单:
pyinstaller main.py
【讨论】:
以上是关于Pyinstaller 可执行文件打开并立即关闭的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Linux 上使用 pyinstaller 和 cefpython 创建可执行文件(ICU 数据的文件描述符无效)