Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error loading Python lib with PyInstaller on MacOS

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'

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

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

like image 959
A. Pine Avatar asked Mar 14 '20 00:03

A. Pine


1 Answers

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

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

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).

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

like image 199
wutangforever Avatar answered Nov 11 '22 10:11

wutangforever