In this tutorial you will learn how to compile standalone exe in python with pyinstaller module. This is great for PyGame and PySide, but works for any python script. The best thing about pyinstaller is that creates one file and you don’t have to have options script file to compile something. Everything can be done through console with single command line.
There are couple of ways you can do this, but as far as I found, things like py2exe, cx_Freeze and others require specific options script file. It just took me too much time, all I wanted to do was simply compile python script to standalone executable without any dependencies and pyinstaller does exactly that.
If you are on windows and you have your python environment installed, open start menu, find cmd (Command Prompt) and use pip to get pyinstaller, type the following command: pip install pyinstaller
At the end of the installation process you should see confirmation message, similar to this:
Successfully installed future-0.16.0 pefile-2016.3.28 pyinstaller-3.2
You should be able to get this installed using: pip install pyinstaller
If it doesn’t work, make sure you have setuptools and pip installed, paste these commands into terminal:
sudo apt-get install python-setuptools
sudo apt-get install python-pip
As of OS X 10.8 Mac comes with Python 2.7 so the process is the same as for windows. Make sure you have pip installed or type: sudo easy_install pip in terminal.
Once you have pip installed, type: pip install pyinstaller
For the tutorial purposes I wrote simple python script, which creates a text file (pythonicways.txt) in current user’s home folder (C:\Users\your_username):
from os.path import expanduser home = expanduser("~") f = open(home + "/pythonicways.txt", 'w') f.write("Compile Standalone exe in Python, visit: https://pythonicways.wordpress.com") f.close()
Compile Standalone executable
In order to get .exe you have to run pyinstaller on Windows, to get Linux or Mac standalone, you have to compile your script on the corresponding platform.
Usually, I would use this line to compile fairly simple python scripts to standalone executables. Run the following command in cmd:
pyinstaller --onedir --onefile --name="MyStandalone" --windowed --icon="C:\fav.ico" "C:\compile.py"
Mine was compiled in couple of seconds and at the end of the process location of the standalone file was displayed. Pyinstaller, by default creates standalone executable in current user’s home folder. My dist location was: C:\Users\MyUsername\dist. Your location should be similar.
Once you have pyinstaller installed, it is as easy as on Windows. Simply open terminal and use the similar command to compile standalone executable:
pyinstaller --onedir --onefile --name="MyStandalone" --windowed --icon="/home/username/fav.ico" "/home/username/compile.py"
Again, the location to standalone executable was printed in terminal, yours should look similar:
3863 INFO: Appending archive to ELF section in EXE /home/username/dist/MyStandalone
If you compiled your python script on Windows as standalone exe and now for some reason you have to run it on linux, ubuntu has a great tool called wine.
To setup wine, type following commands in terminal:
sudo add-apt-repository ppa:ubuntu-wine/ppa
sudo apt-get update
sudo apt-get install wine
As of now, if everything went well, you should be able to run windows executable through linux terminal with command: wine “/home/username/path/to/file.exe”
The same goes for Mac, simply open terminal and paste the following command. You only have to adjust icon and python script file paths.
pyinstaller --onedir --onefile --name="MyStandalone" --windowed --icon="/Users/username/Documents/fav.ico" "/Users/username/Documents/compile.py"
Compiled standalone was saved in “/Users/username/Documents/dist/MyStandalone”, you should be able to find yours in similar folder.
–onedir – Create a one-folder bundle containing an executable (default)
–onefile – Create a one-file bundled executable.
–name – Define name for the executable.
–windowed – Prevents a console window from being displayed when the application is run.
–icon=”C:\fav.ico” – Location to icon file that’s going to be used in standalone .exe
“C:\compile.py” – Location to your python script file.
If you have any questions, leave a comment. If everything went well, share this post to spread the knowledge. It shouldn’t take more than 5 minutes for someone to compile python script file to standalone executable.