This is a record of generating an executable file from Python code using pyinstaller on a Mac.
print_sys_path.py
import sys
print(sys.path)
print(f"__name__ = {__name__}")
print(f"__file__ = {__file__}")
Using the pyinstaller installed with pip, I ran the following on my terminal:
pyinstaller print_sys_path.py --onefile
I got a Python library not found error as below.
71 INFO: PyInstaller: 3.6
71 INFO: Python: 3.7.4
83 INFO: Platform: Darwin-19.2.0-x86_64-i386-64bit
(Omitted)
OSError: Python library not found: libpython3.7m.dylib, .Python, libpython3.7.dylib, Python
This would mean your Python installation doesn't come with proper library files.
This usually happens by missing development package, or unsuitable build parameters of Python installation.
* On Debian/Ubuntu, you would need to install Python development packages
* apt-get install python3-dev
* apt-get install python-dev
* If you're building Python by yourself, please rebuild your Python with `--enable-shared` (or, `--enable-framework` on Darwin)
Use "--enable-shared" as an option when installing Python with pyenv. I did the following on my terminal:
PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.4
The following is displayed and 3.7.4 has been overwritten.
pyenv: /Users/username/.pyenv/versions/3.7.4 already exists
continue with installation? (y/N) y
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.4.tar.xz...
-> https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
Installing Python-3.7.4...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.7.4 to /Users/username/.pyenv/versions/3.7.4
Check the Python used.
$ pyenv global
3.7.4
When I started pyinstaller, I still got the Python library not found error as below.
$ pyinstaller print_sys_path.py --onefile
(Omitted)
OSError: Python library not found: Python, libpython3.7.dylib, .Python, libpython3.7m.dylib
This would mean your Python installation doesn't come with proper library files.
This usually happens by missing development package, or unsuitable build parameters of Python installation.
* On Debian/Ubuntu, you would need to install Python development packages
* apt-get install python3-dev
* apt-get install python-dev
* If you're building Python by yourself, please rebuild your Python with `--enable-shared` (or, `--enable-framework` on Darwin)
I was scolded that the .dylib file could not be found, so use the find command to find the location as shown below.
find $HOME -name 'libpython3*.dylib' 2> /dev/null
I specified the location of libpython3.7m.dylib in the LD_LIBRARY_PATH environment variable and ran it, but the Python library not found error remained as shown below.
$ LD_LIBRARY_PATH=/Users/username/.pyenv/versions/3.7.4/lib pyinstaller print_sys_path.py --onefile
(Omitted)
OSError: Python library not found: .Python, Python, libpython3.7m.dylib, libpython3.7.dylib
This would mean your Python installation doesn't come with proper library files.
This usually happens by missing development package, or unsuitable build parameters of Python installation.
* On Debian/Ubuntu, you would need to install Python development packages
* apt-get install python3-dev
* apt-get install python-dev
* If you're building Python by yourself, please rebuild your Python with `--enable-shared` (or, `--enable-framework` on Darwin)
Use "--enable-framework" as an option when installing Python with pyenv. I did the following on my terminal:
PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.7.4
The following is displayed and 3.7.4 has been overwritten.
pyenv: /Users/username/.pyenv/versions/3.7.4 already exists
continue with installation? (y/N) Y
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.4.tar.xz...
-> https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
Installing Python-3.7.4...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.7.4 to /Users/username/.pyenv/versions/3.7.4
When I started pyinstaller, I got the following "pyinstaller not found" error.
$ LD_LIBRARY_PATH=/Users/username/.pyenv/versions/3.7.4/lib pyinstaller print_sys_path.py --onefile
-bash: /Users/username/.pyenv/shims/pyinstaller: No such file or directory
When I tried to reinstall pyinstaller, I was told that it was "already" as shown below.
$ pip install pyinstaller
Requirement already satisfied: pyinstaller in /Users/username/.pyenv/versions/3.7.4/lib/python3.7/site-packages (3.6)
Requirement already satisfied: altgraph in /Users/username/.pyenv/versions/3.7.4/lib/python3.7/site-packages (from pyinstaller) (0.17)
Requirement already satisfied: setuptools in /Users/username/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pyinstaller) (40.8.0)
Requirement already satisfied: macholib>=1.8 in /Users/username/.pyenv/versions/3.7.4/lib/python3.7/site-packages (from pyinstaller) (1.14)
I don't understand why! As a result of trial and error, I was able to reinstall pyinstaller by following the steps below: Uninstall → Install.
$ pip uninstall pyinstaller
Uninstalling PyInstaller-3.6:
Would remove:
/Users/username/.pyenv/versions/3.7.4/lib/python3.7/site-packages/PyInstaller-3.6.dist-info/*
/Users/username/.pyenv/versions/3.7.4/lib/python3.7/site-packages/PyInstaller/*
Proceed (y/n)? Y
Successfully uninstalled PyInstaller-3.6
$ pip install pyinstaller
Processing /Users/username/Library/Caches/pip/wheels/62/fe/62/4c0f196d1e0dd689e097449bc81d7d585a7de7dd86b081b80b/PyInstaller-3.6-cp37-none-any.whl
Requirement already satisfied: setuptools in /Users/username/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pyinstaller) (40.8.0)
Requirement already satisfied: macholib>=1.8 in /Users/username/.pyenv/versions/3.7.4/lib/python3.7/site-packages (from pyinstaller) (1.14)
Requirement already satisfied: altgraph in /Users/username/.pyenv/versions/3.7.4/lib/python3.7/site-packages (from pyinstaller) (0.17)
Installing collected packages: pyinstaller
Successfully installed pyinstaller-3.6
When I started pyinstaller again as shown below, an executable file was generated under the dist directory. I specified LD_LIBRARY_PATH, but it seems to work even if I don't specify it.
LD_LIBRARY_PATH=/Users/username/.pyenv/versions/3.7.4/lib pyinstaller print_sys_path.py --onefile
Launched the generated executable (successful).
dist/print_sys_path
['/var/folders/kc/blch89657dv8zrzlxjljz6f40000gr/T/_MEInZMCQo/base_library.zip', '/var/folders/kc/blch89657dv8zrzlxjljz6f40000gr/T/_MEInZMCQo']
__name__ = __main__
__file__ = print_sys_path.py
The first --enable-shared failed, but when I installed it again with --enable-shared, it succeeded, so I'll record it.
Installing Python with pyenv:
$ PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.4
pyenv: /Users/username/.pyenv/versions/3.7.4 already exists
continue with installation? (y/N) y
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.4.tar.xz...
-> https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
Installing Python-3.7.4...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.7.4 to /Users/username/.pyenv/versions/3.7.4
Start pyinstaller:
pyinstaller print_sys_path.py --onefile
Launch the generated executable:
$ dist/print_sys_path
['/var/folders/kc/blch89657dv8zrzlxjljz6f40000gr/T/_MEIf6CW7m/base_library.zip', '/var/folders/kc/blch89657dv8zrzlxjljz6f40000gr/T/_MEIf6CW7m']
__name__ = __main__
__file__ = print_sys_path.py
Try it with pipenv. Start pyinstaller:
pipenv run pyinstaller print_sys_path.py --onefile
Launch the generated executable:
$ dist/print_sys_path
['/var/folders/kc/blch89657dv8zrzlxjljz6f40000gr/T/_MEIPYS34b/base_library.zip', '/var/folders/kc/blch89657dv8zrzlxjljz6f40000gr/T/_MEIPYS34b']
__name__ = __main__
__file__ = print_sys_path.py
Probably, if you overwrite Python with pyenv, you will need to reinstall (uninstall → install) what was installed with pip. Solution 1 I installed Python by overwriting it, but pyinstaller used the one that was included before, so the same error probably occurred.
Recommended Posts