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

A. *_*ine 8 python macos pyinstaller python-3.x

我正在尝试将一些 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'
Run Code Online (Sandbox Code Playgroud)

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

现在,我试图打包的脚本只包含print('hello world!')我正在使用的打包命令pyinstaller -F hello_world.py

小智 2

我成功使用pyenv并使用启用框架选项安装了特定版本的 python:

env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.7.6

pyenv local 3.7.6使用此版本的 python(使用hello_world.py 所在文件夹中的命令进行设置),使用pip. 然后构建

pyinstaller hello_world.py --onefile --clean --windowed

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

也可以在虚拟环境中完成所有这些(通过pyenv-virtualenv轻松管理)。