So, you just finish a program you write in Python and you want to share it with friends, family and upload it to the internet. Problem is, a lot of people aren't going to know what to do with a .py or .pyc file. So the solution is to make an exe file. There are several 'programs' that can be used to make a .exe file from a .py file. Some of them include; Pyinstaller, Py2exe, Gui2exe, cx_Freeze just to name a few. For me creating my first .exe file was a big task. I struggled to find a suitable program to do it and didn't know how to use it when I did. Unfortunately for me, py2exe does not work on my computer so I was unable to use it. So I am going to show you how to use pyinstaller to make an .exe file. There is a help guide for using pyinstaller, but for some of the features I found that guide confusing.
Getting Pyinstaller
First of all, you have to download pyinstaller. You can download it from here:.
After downloading the file, you need to unzip it. It should extract to a pyinstaller-1.4 folder. You don't need to install pyinstaller and it doesn't need to go in the usual Python\lib\site-packages. For the sake of this tutorial, I will use pyinstaller as if it was on the desktop.
Preparing your script
Before you make your .py file a .exe file you want to make sure it works. So run the script and make sure it works. For this tutorial I will use the following script for examples:
import sys print "Hello World!" sys.exit()
I have purposely imported the sys module for something later. For me, I often use PythonCard for my GUI's. For pyinstaller, you need to add a few imports to your script. So, for example if I make a GUI with a button that says 'Hello' and some static text which says 'Goodbye', I need to have the following in my script for pyinstaller:
from PythonCard.components import button, statictext
This could apply to other GUI's and modules so if you keep getting weird errors where things aren't working, make sure you don't need to do an import thing like shown above. The reason you have to have imports that you usually wouldn't need is because when pyinstaller runs, it imports all the dependencies of your script and in a GUI, all the parts need to be there for them to show up on the GUI.
This mainly applies to PythonCard because there is a separate GUI file but could apply to other modules so I thought I should mention it.
Making an exe file
Ok, so you've made sure your script works so it's time to make the exe file. For this, I am assuming that pyinstaller is on the computer desktop. It doesn't have to be there it just makes it easier. So, open up the command prompt by going start>all programs>accessories or clicking run and typing cmd. For windows 7, you can just type cmd into the start search bar. When the cmd is open, you need to set it to where pyinstaller is. So for the desktop we enter the following:
cd desktop\pyinstaller-1.4
That sets it so it operates from that directory for when you launch the functions of pyinstaller.
Then run Configure.py to set up pyinstaller.
Now, you need to copy your script (.py file) and put in in the pyinstaller directory. If you have a custom module/dependency which isn't in the lib\site-packages, you need to put that with the .py file when you copy it. If you want to have an icon for your program, it has to be a .ico file and it must also be put with the .py file. If you don't use an your own icon, you will get the default icon of a blue butterfly. If you need to convert a picture (.jpg, .gif, .png, .bmp etc) to a .ico you can use the following website:
convert icon
Ok, for this bit, I am going to make a single .exe file with an icon and no command console.
In the cmd, enter in the following:
Makespec.py --icon=icon.ico --onefile --noconsole -nProgramName Program.py
(if you are using the script I have used above as an example, you need to change --noconsole to --console.
It should say something like:
wrote C:\...ProgramName.spec
now run Build.py to build the exe.
So, now we run Build.py to make the exe file. You can run it by entering this:
Build.py ProgramName\ProgramName.spec
It will come up with a whole bunch of stuff about what it is doing in making the exe file.
then it should tell you it appended the .exe file to a directory.
Go to that directory and then go to the folder called dist. In the folder you should have a single .exe file and it should have your icon as the image. Double click it and see if it runs correctly.
One thing you may notice about the .exe file is that it is usually over 1mb big, depending on your script. This is because all of the python launchers and readers need to be packaged into the .exe file. For me on a standard 350 line script (with a GUI) that is about 10kb, after running through pyinstaller it works out to be about 8.4mb which is quite large seeing as all the program does is move some files. You can use upx if you need a smaller .exe file, but I have found it doesn't really help much. It changed my 8.4mb .exe to 8.3mb.
All of the options you enter into the cmd for the Makespec.py are optional arguments. You don't have to have an icon or only a single file. For more information about the arguments you can look here:
Pyinstaller Manual
Some of the options are only available on certain platforms (e.g the --noconsole is windows only) so be careful!
good luck making .exe files!
Happy Coding!





MultiQuote





|