Pyinstaller 可执行文件打开并立即关闭

Pyinstaller executable opens and immediately closes

我正在尝试使用 pyinstaller 从 python 项目创建可执行文件。但是,当我 运行 可执行文件时,它会立即打开和关闭。 从命令行 运行ning 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'

我被这个问题困了好几天了,现在我真的不知道该怎么办了!


我的系统规格:

我运行在虚拟环境中设置所有内容。

我的项目结构是:

Project  
|- dist  
|- build  
|- main.spec  
|  
|- main.py  
|- main  
|  |- GUI  
|     |- MainWindow.py  
|     |- ui.py  
|  |- Sim  
|     |- Setter.py  
|     |- Integrator.py  
   

在我将目录 main 的名称从 main 更改为 MainPackage 后,可执行文件现在可以运行了。

我认为错误是我的入口点 main.py 与目录 main 同名(其中包含调用的其他代码)。所以pyinstaller混淆了目录和入口文件。

此外,我不必为可执行文件的工作包含两个隐藏的导入(正如所怀疑的那样)。构建可执行文件的命令很简单:

pyinstaller main.py