Converting python script into an executable
07 Nov 2015 #python #guiBackDrop:
So recently I was building a Calculator using tkinter
.
I wrote a blog post for the same some time back.
Now I thought, how awesome it would be if I could distribute it to my friends and let the use it. Problem was that some of them not being CS grads would not know head or tails about how to run it!
So I thought the best way and the easiest way was to convert the pyCalc.py
into a .exe
file.
That way both my purposes were solved.
- non-cs people would get an interface to run it which was familiar to them. Heck even a granny who knows how to use chrome to watch cookery shows can use it now. Ok, that was a little bit too much
- They didn’t have to install anything in this process
Enter pyinstaller
Note: Before installing PyInstaller on Windows, you will need to install
PyWin32
. You do not need to do this for GNU/Linux or Mac OS X systems.
To install it. You just have to do
$ sudo pip install pyinstaller
for python2.*
or
$ sudo pip3 install pyinstaller
for python3.*
If you are behind a proxy server, just add -E
flag like this sudo -E pip3 ..
Creating the executable
I have my pyCalc.py
which I want to make an executable of, in
To build the executable
Yes, it’s that easy!
If you are not haunted with any errors. You should see two folders being placed in pyCalc
For a successful build , the final executable, pyCalc
, and any associated files, will be placed in the dist
directory, which will be created if it doesn’t exist.
Let me briefly describe the options that are being used:
-
--onefile
is used to package everything into a single executable. If you do not specify this option, the libraries, etc. will be distributed as separate files alongside the main executable. -
--windowed
prevents a console window from being displayed when the application is run. If you’re releasing a non-graphical application (i.e. a console application), you do not need to use this option. -
pyCalc.py
the main source file of the application. The basename of this script will be used to name of the executable, however you may specify an alternative executable name using the--name
option.
See the PyInstaller Manual for more configuration information.
Sadly, we can’t make windows
executables from pyinstaller
! It supported it some time back in the earlier versions but not in the newer versions.
Alternatives to pyinstaller
- http://www.py2exe.org/
- http://nuitka.net/
- http://wiki.python.org/moin/Py2Exe
- http://cx-freeze.sourceforge.net/
Till then. Goodbye!