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
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).
The latest version of Pyinstaller only supports Python 2.7, 3.5–3.7. If you downgrade to Python 3.7 it will work!
Related
I'm currently trying to run a file called forum.py in the vagrant directory but it returns an ImportError: No module named flask. After I logged out vagrant, when I type flask --version, the python version is 3.9.1 and the flaks version is 2.0.2. When I check the python version in vagrant, it returns 2.7.12. When I type flask-version, it returns flask: command not found. Does it mean the flask is installed to python 3.9 but not 2.7? Moreover, when I make a new directory in IDE for the files, should I choose Python3.9 or Python2.7 as my interpreter?
Ideally, together with your file, you have a requirements.txt for you to install the project dependencies on your machine, or better yet, on your virtual environment
Btw, if you are going to use your Global version of Python... Try to use the most current version.
Try the command: python3 forum.py
I have been trying to build .exe file using pyinstaller in windows 10. It worked, but the size of the exe file is ~212 MB, even by using a venv (as in here). I thought it might be because I am using python by anaconda!
Then I installed a separate version of Python so not to use anaconda! But it did not work (still large file).
Then I uninstalled anaconda to test it. Pyinstaller is still trying to access Python in 'C:\Program Files\anaconda3\python.exe' (this error: No Python at 'C:\Program Files\anaconda3\python.exe'). However I have removed all path to anaconda. Probably it has always tried to reach anaconda, and this is why I haven't been successful to build a small size .exe file.
How can I clearly indicate paths for pyinstaller and python?
Finally, after a lot of researching, could solve my problem:
Uninstalled all pythons and anaconda from my PC
Removed all Path from the system variables
Restarted the windows
Installed a fresh Python from its website
Installed Pyinstaller using pip install pyinstaller
Tested my .py code in cmd. It showed me all the packages that are missing.
Installed all required packages by using pip install name-of-package
Ran final command by pyinstaller -F -w --clean file.py
(Optional) Install Anaconda if you need (don't add Anaconda Python as the default python. Also don't add its path to the system variables).
Note: You can build virtualenv and do pyinstaller in them.
My previous tries which used anaconda resulted in file of 212 MB in size. This process generated a .exe file of size 27 MB (Importing only pandas module).
I ran into a similar problem and found PyCharms virtualenv manager very helpful. https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
This just necessitated downloading python from python.org and linking the virtual environment to this interpreter, rather than the conda interpreter (otherwise it will throw strange SSL errors).
This seems to allow neat parallel use of conda and virtualenv.
I am trying to convert a .py file to an exe. My file, hello.py, reads:
print "Hello, World!"
I am currently trying to use pyinstaller. However when I run the command
pyinstaller hello.py
I get the error message "tuple index out of range" which I have been told means my version of python is unsupported. In particular it would seem the situation is that pyinstaller thinks I am trying to compile python 3.6 code into an exe. But I have python 2.7 and python 3.6 installed. How do I let it know that I want it to regard the code as python 2.7 code?
Using Python3:
Make sure PyInstaller is installed in Python 3.x: pip3 freeze
PyInstaller==3.3.1
Then running the command:
/path/to/python3 -m PyInstaller your_script.py
First install Pyinstaller in your python2.7 version if not installed previously
py -2 -m pip install pyinstaller
and then go to your folder and
py -2 -m pyinstaller -F filename.py
When you need to bundle your application within one OS but for different versions of Python and support libraries – for example, a Python 3 version and a Python 2.7 version; or a supported version that uses Qt4 and a development version that uses Qt5 – we recommend you use virtualenv. With virtualenv you can maintain different combinations of Python and installed packages, and switch from one combination to another easily. (If you work only with Python 3.4 and later, python3 -m venv does the same job, see module venv.)
Use virtualenv to create as many different development environments as you need, each
with its unique combination of Python and installed packages.
Install PyInstaller in each environment.
Use PyInstaller to build your application in each environment.
The answers have devolved in "How do I tell pyinstaller which python version to use?" So, I know that this doesn't really answer the original question. However, I wasted about an hour trying to figure this out so, in the hopes that others don't have to waste an hour... To force pyinstaller to use Python 3.9 under Windows do the following.
Given Windows and Python 3.9:
python3.9 -m PyInstaller [whatever options you want]
You have to type it as PyInstaller instead of pyinstaller (i.e. note the capitalization).
python3.9 -m pyinstaller
Produces an error:
C:\Users\chris.SR\AppData\Local\Programs\Python\Python39\python.exe: No module named pyinstaller
Supposing you have python 2.x on the path under python2 you can do
python2 -m pyinstaller hello.py
I ran two a couple things.
If you uninstall python3, it works with python2.
If you have python3 installed (and it is the primary), and have pyinstaller installed in python3, it wont work (python3 pyinstaller used).
If you have python3 installed, but do not have it installed in python3 or uninstalled it (pip3 uninstall pyinstaller), pyinstaller works.
Checking the environmental variables (windows 10) PATH had python3 first. This may be the issue and may not be resolved because it is checking python3 directories first, and picks up pyinstaller for python3. pyinstaller does not check the file either (#!/usr/env/bin python2).
Unless pyinstaller puts an option relating to this issue, there may be no solution short of uninstalling pyinstaller from python3 temporarily.
note
could also use py2exe, using py2exe for python2, pyinstaller for python3
I would like to make a Mac executable for my Python code but always got this error message:
OSError: Python library not found: .Python, libpython3.5.dylib, Python
Both PyInstaller and py2app returned the same error. What should I do to overcome this problem?
For Mac, try rebuild python to generated shared lib using:
env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.5.x
as said in pyenv offical doc
in which 3.5.x is version of python which you are using
For my here, after reinstall python:
env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.8.0
the dylib located in
~/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/libpython3.8.dylib
which is soft link to 3.4MB file:
~/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/Python
I have downloaded py2app, but the problem is that easy install seems to be an online installation, yet I am installing this on an off-line pc, so can't use easy install. I expected to be able to download an EXE file or MSI file to install it on my PC using a normal procedure to install a Python package, because the python packages that I have installed before have been from self running files.
The downloadable version of Py2app does not include any such self running file. It tells me to type $python setup.py install. Where do I type this? Into what command line?
If you're running Windows (and it seems that you're) you will need to go to the Run window (Win + R) and...
type cmd
type python setup.py install
...to begin the instalation.
If you doesn't have Python on your PATH, the use:
C:\PythonXY\python.exe setup.py install (for example, in Python 2.7 use C:\Python27\python.exe setup.py install).
Also maybe you need to specify the setup.py path so you should do:
C:\PythonXY\python.exe C:\Some\Path\setup.py install.
If you're running any Linux distro then just open the terminal and type that command.
Hope it helps!
You need to download an install the following packages (in this order):
altgraph
macholib
modulegraph
py2app
All of them can be installed by first downloading and extracting the archive and then run "python setup.py install" with the current working directory set to the directory containing the setup.py file.
However... You appear to want to install py2app on a Windows PC, and that won't work because py2app does not support Windows (it cannot cross "compile" a Mac application bundle).