pyinstaller 未找到隐藏导入

xen*_*ndi 4 python pyinstaller python-import toml

我正在使用 pyinstaller。我的脚本中有:

import toml


config = toml.load('config.toml')
Run Code Online (Sandbox Code Playgroud)

我用以下方法编译了我的脚本:

pyinstaller main.py --onefile --clean --name myApp

但是当我运行可执行文件时,它给了我:ModuleNotFoundError: No module named 'toml'

所以我尝试了这个:

pyinstaller main.py --hidden-import toml --onefile --clean --name myApp

现在 pyinstaller 说:ERROR: Hidden import 'toml' not found

xen*_*ndi 13

找到了答案。如果您使用虚拟环境(如 Pipenv、pyenv、venv),则需要在该环境的上下文中运行 pyinstaller。所以...

pip install pyinstaller
python -m PyInstaller main.py ....
Run Code Online (Sandbox Code Playgroud)

另外,正如 mosegui 指出的那样,您应该将配置标志放在文件名之前:

pyinstaller --hidden-import toml --onefile --clean --name myApp main.py
Run Code Online (Sandbox Code Playgroud)

尽管这已经是很久以前的事了,我不确定这对我来说是否真的是一个问题。

这些天我使用诗歌,所以一旦我有了诗歌环境,我就会poetry shell和/或poetry run pyinstaller ...。无论何时使用,poetry run <some cmd sequence>它都会在当前虚拟环境的上下文中运行您的命令序列。我相信pipenv run可以完成类似的事情,但诗歌对我来说总是更有效。