在MacOS上使用PyInstaller加载Python库时出错 [英] Error loading Python lib with PyInstaller on MacOS

查看:193
本文介绍了在MacOS上使用PyInstaller加载Python库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to package some python into an executable on MacOS (10.14.5). I am able to create the executable, but executing the resulting dist/hello_world executable gives the following error:

[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'

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

My machine is running Python 3.7.7 installed from using the Mac installer downloaded from https://www.python.org/

目前,我要打包的脚本仅包含print('hello world!'),而我正在使用的打包命令是pyinstaller -F hello_world.py

For now, the script I am trying to package only contains print('hello world!') and the packaging command I am using is pyinstaller -F hello_world.py

推荐答案

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

I had success using pyenv and installing the specific version of python with the enable framework option:

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

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

With this version of python (set using the command pyenv local 3.7.6 in the folder where hello_world.py is located), install pyinstaller (and any other modules you need) with pip. Then build with

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

,这将为您提供正常运行的dist/hello_word.作为python的雏鸟,我不知道为什么这样做会与原始问题中的步骤相反. (我以为Mac OS安装程序默认情况下包括Python.framework -我确信我的无知在这里暴露了出来,因为这可能与上面的PYTHON_CONFIGURE_OPTS变量有很大不同.)

which should give you a functioning dist/hello_word. As a python fledgling, I have no idea why this works as opposed to the steps in the original question. (I thought the Mac OS installer included the Python.framework by default -- I'm sure my ignorance is exposed here as this probably differs significantly from what the PYTHON_CONFIGURE_OPTS variable is doing above).

还可以在虚拟环境中进行所有操作(由 pyenv-virtualenv 轻松管理).

Also possible to do all this within a virtual environment (easily managed by pyenv-virtualenv).

这篇关于在MacOS上使用PyInstaller加载Python库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆