Python is shipped by default in Linux and Mac. So deploying a python app is quite easy on these platforms. But it remains a pain in you-know-where when you try to distribute your app among Windows users. Windows people are usually habituated to getting exes. So, with a little effort and a very useful tool, we serve them what they desire.
There’s this PyInstaller utility which converts python scripts into exes. You can create a directory installation (one single executable and other necessary files in a directory) or just a plain exe everything embedded into it. If UPX is available on your system, you can take advantage of that too. So, how do we achieve this?
# First get Python from http://python.org [Probably you already have it]
# Get Pywin32 package. You must match the version of PyWin32 corresponding to your Python version. I am using Python 2.5, so in my case, I must use the pywin32 package which has been built for Python 2.5.
# Get PyInstaller from http://www.pyinstaller.org/
# Extract the pyinstaller content and add to path. You have to right click on My Computer, Go to Properties. Go to Advanced System Settings > Environment Variables. Modify the “PATH” variable and add the location of the extracted pyinstaller directory. Now we’re ready!
# When we first time use PyInstaller, we must generate the configuration by typing this command:
1 |
Configure.py |
It will generate a configuration file in the pyinstaller root directory.
# Now to compile a python script, use this command:
1 2 |
Makespec.py < file name/path > [options] Build.py < filename >.spec |
You can find full documentation under the “docs” folder of the pyinstaller directory. Please note the different options to pass to the Makespec.py file to change the behaviour of the executable. For example: I do like this
1 |
Makespec.py main.py -F -w |
The -F parameter makes it a single executable with everything embedded inside. The -w parameter makes it a windowed app so no console is displayed. I need it specially when compiling a GUI app written in PyQT 🙂
So thats it, quite simple! Have fun 🙂
3 replies on “Compiling Python Apps to Exe on Windows”
Planning to try that into wine. BTW, does that also embeds the python interpreter ?
Installed all in wine. And it successfully built an exe. But when running the exe it shows Traceback (most recent call last):
File “”, line 26, in
File “C:\Pyinstaller\iu.py”, line 455, in importHook
raise ImportError, “No module named %s” % fqname
ImportError: No module named PyQt4
😛 😛 i forgot to install pyqt in wine 😛
Yes, it does 🙂