简体   繁体   English

pyinstaller 找不到隐藏的导入

[英]pyinstaller Hidden import not found

I'm using pyinstaller.我正在使用 pyinstaller。 In my script there is:在我的脚本中有:

import toml


config = toml.load('config.toml')

I compiled my script with:我编译了我的脚本:

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

but when I run the executable it gave me: ModuleNotFoundError: No module named 'toml'但是当我运行它给我的可执行文件时: ModuleNotFoundError: No module named 'toml'

So I tried this:所以我尝试了这个:

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

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

Found the answer.找到了答案。 If you are using a virtual environment (Like Pipenv, pyenv, venv) you need to run pyinstaller in the context of that environment.如果您使用的是虚拟环境(如 Pipenv、pyenv、venv),则需要在该环境的上下文中运行 pyinstaller。 So...所以...

pip install pyinstaller
python -m PyInstaller main.py ....

Also, as mosegui pointed out, you should put your config flags before the file name:此外,正如 mosegui 所指出的,您应该将配置标志放在文件名之前:

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

though this was so long ago that I'm not sure if that was actually an issue for me.虽然这是很久以前的事了,我不确定这对我来说是否真的是一个问题。

These days I use Poetry so once I have a Poetry environment I just poetry shell and/or poetry run pyinstaller ... .这些天我使用Poetry,所以一旦我有了 Poetry 环境,我就只需要poetry run pyinstaller ... poetry shell和/或poetry run pyinstaller ... Anytime you use poetry run <some cmd sequence> it runs whatever your command sequence is in the context of the current virtual environment.每当您使用poetry run <some cmd sequence>它都会在当前虚拟环境的上下文中运行您的任何命令序列。 I believe pipenv run accomplishes a similar thing but Poetry always works better for me.我相信pipenv run完成了类似的事情,但 Poetry 对我来说总是更好。

I know this is a very belated answer, but I will just leave an observation here in case someone finds himself/herself in a similar situation:我知道这是一个非常迟到的答案,但我会在这里留下一个观察,以防有人发现自己处于类似情况:

Even if you have toml installed, pyinstaller will not find the hidden import because you are passing the config flags after your script name, instead of before, so the command executes up until your script name and disregards the rest.即使您安装了tomlpyinstaller也不会找到隐藏的导入,因为您在脚本名称之后而不是之前传递配置标志,因此该命令会一直执行到您的脚本名称并忽略其余部分。 Try:尝试:

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

instead of your current:而不是你当前的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM