在MacOS上使用PyInstaller加载Python库时出错

2024-09-27 02:25:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将一些python打包到MacOS(10.14.5)上的可执行文件中。我可以创建可执行文件,但执行生成的dist/hello_world可执行文件会出现以下错误:

[55240] Error loading Python lib '/var/folders/yh/6_6mb2y96kg0gnb_nh9r2zrr0000gp/T/_MEIwUMw4X/Python': dlopen: dlopen(/var/folders/yh/6_6mb2y96kg0gnb_nh9r2zrr0000gp/T/_MEIwUMw4X/Python, 10): no suitable image found.  Did find:
    /var/folders/yh/6_6mb2y96kg0gnb_nh9r2zrr0000gp/T/_MEIwUMw4X/Python: code signature invalid for '/var/folders/yh/6_6mb2y96kg0gnb_nh9r2zrr0000gp/T/_MEIwUMw4X/Python'

我的机器运行的是使用从https://www.python.org/下载的Mac安装程序安装的Python 3.7.7

目前,我尝试打包的脚本只包含print('hello world!'),我使用的打包命令是pyinstaller -F hello_world.py


Tags: 可执行文件helloworldvarlibdist错误macos
2条回答

我成功地使用了pyenv并使用enable framework选项安装了python的特定版本:

env PYTHON_CONFIGURE_OPTS=" enable-framework" pyenv install 3.7.6

使用此版本的python(使用hello_world.py所在文件夹中的命令pyenv local 3.7.6进行设置),使用pip安装pyinstaller(以及您需要的任何其他模块)。然后用

pyinstaller hello_world.py onefile clean windowed

这应该给你一个功能性的dist/hello_word。作为一个初出茅庐的python新手,我不知道为什么与原始问题中的步骤相反,这是可行的。(我认为Mac OS安装程序默认包含Python.framework,我确信我的无知在这里暴露了出来,因为这可能与上面的PYTHON_CONFIGURE_OPTS变量所做的有很大不同)

也可以在虚拟环境中执行所有这些操作(由pyenv-virtualenv轻松管理)

Pyinstaller的最新版本仅支持Python 2.7、3.5–3.7。如果您降级到Python3.7,它将工作

相关问题 更多 >

    热门问题