使用 Pyinstaller 创建的二进制文件失败:使用 Pipenv 时 "No module named ..."、"Failed to execute script ..."
Binary created with Pyinstaller fails: "No module named ...", "Failed to execute script ..." when using Pipenv
从简单的 Python 脚本创建可执行文件时,Pyinstaller 成功完成。但是当尝试 运行 可执行文件时,它失败并显示 ModuleNotFoundError
:
Traceback...
ModuleNotFoundError: No module named 'git'
[26228] Failed to execute script xyz
我已经通过 Pipenv 安装了 gitpython。为什么捆绑不正确?
此处可能存在的问题是使用全局 Pyinstaller 安装。本次安装不知道用Pipenv下载的pip包。
我在 Pipenv shell 中再次尝试并且效果很好:
pipenv shell
pip install pyinstaller
pyinstaller script.py -F
现在模块已正确捆绑。
编辑: 我刚刚了解到我的回答并不完全正确。 Aarion 的 修正了我的错误。
@vauhochzett 提供的答案几乎是正确的,但需要进行如下修改。不要在 pipenv 中使用 pip 安装 pyinstaller,而是使用 pipenv 本身安装 pyinstaller。
这意味着您需要将 pyinstaller 安装到您创建的每个 pipenv 中,但它会为您使用的每个 venv 正确整理导入路径。
当使用 pip 安装 pyinstaller 时,导入路径出现问题,但当使用 pipenv 在 venv 中安装模块时,一切正常,如下所示。
$ pipenv install pyinstaller
$ pipenv run pyinstaller script.spec
从简单的 Python 脚本创建可执行文件时,Pyinstaller 成功完成。但是当尝试 运行 可执行文件时,它失败并显示 ModuleNotFoundError
:
Traceback...
ModuleNotFoundError: No module named 'git'
[26228] Failed to execute script xyz
我已经通过 Pipenv 安装了 gitpython。为什么捆绑不正确?
此处可能存在的问题是使用全局 Pyinstaller 安装。本次安装不知道用Pipenv下载的pip包。
我在 Pipenv shell 中再次尝试并且效果很好:
pipenv shell
pip install pyinstaller
pyinstaller script.py -F
现在模块已正确捆绑。
编辑: 我刚刚了解到我的回答并不完全正确。 Aarion 的
@vauhochzett 提供的答案几乎是正确的,但需要进行如下修改。不要在 pipenv 中使用 pip 安装 pyinstaller,而是使用 pipenv 本身安装 pyinstaller。
这意味着您需要将 pyinstaller 安装到您创建的每个 pipenv 中,但它会为您使用的每个 venv 正确整理导入路径。
当使用 pip 安装 pyinstaller 时,导入路径出现问题,但当使用 pipenv 在 venv 中安装模块时,一切正常,如下所示。
$ pipenv install pyinstaller
$ pipenv run pyinstaller script.spec