首页 > 解决方案 > Pyinstaller 在不存在的文件夹中搜索

问题描述

我一直在尝试使用 pyinstaller 从 python 脚本构建可执行文件,但是,一旦我运行它

pyinstaller --onefile stock_visual.py

在 windows cmd 中,并生成文件,每当我运行它时,我都会收到以下错误:

Traceback (most recent call last):
  File "stock_visual.py", line 6, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\users\verdi\github\jiostocks\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "lib\site-packages\dash_core_components\__init__.py", line 12, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Verdi\\AppData\\Local\\Temp\\_MEI112322\\dash_core_components\\package-info.json'
[4824] Failed to execute script stock_visual

现在,我看到有几个人遇到了类似的问题,但他们是由于需要手动导入动态导入的库,就像这里的 pyinstaller 文档中解释的那样:

https://pythonhosted.org/PyInstaller/when-things-go-wrong.html#listing-hidden-imports

这在我的情况下似乎不起作用,甚至没有创建它正在寻找 dash_core_components 文件的目录: 文件夹截图

附带说明一下,如果我不使用 --onefile 选项,则会出现同样的问题,但该文件夹位于项目内部,位于 \build 文件夹中,但程序查找库的目录也不存在。

到目前为止,我已经尝试了 --onedir 选项,包括我之前所说的手动库,pyinstaller 故障排除网页中所说的所有内容,以及 py2exe 和 cx freezer 替代品,它们也给出了自己的错误。

我尝试使用构建器的脚本可以在这个 repo中找到,我已经用其他脚本尝试过它并且运行没有任何问题。

我正在使用 windows10 64 位,python3.8 和一个封闭的虚拟环境(venv),其中包含在该存储库中的 requirements.txt 文件中列出的所有库。

任何有关如何解决此问题的提示将不胜感激。

标签: pythonpyinstallerhyphen

解决方案


您需要为dash_***模块创建一个挂钩文件。以下可能会起作用:

# hooks/hook-dash_core_components.py

from PyInstaller.utils.hooks import collect_all

datas, binaries, hiddenimports = collect_all('dash_core_components')

将其保存在文件hook-dash_core_components.py旁边的目录下的文件中:

- myfile.py
- hooks
  - hook-dash_***.py

并在编译时添加一个选项;--additional-hook-dir=hooks.


推荐阅读