Converting python script into an executable

Converting python script into an executable

BackDrop:

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.

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

tasdik@Acer:~/Desktop/pyCalc$ tree
.
└── pyCalc.py

0 directories, 1 file
tasdik@Acer:~/Desktop/pyCalc$

To build the executable

tasdik@Acer:~/Desktop/pyCalc$ pyinstaller --onefile --windowed pyCalc.py

Yes, it’s that easy!

If you are not haunted with any errors. You should see two folders being placed in pyCalc

tasdik@Acer:~/Desktop/pyCalc$ tree
.
├── build
│   └── pyCalc
│       ├── base_library.zip
│       ├── localpycos
│       │   ├── pyimod01_os_path.pyc
│       │   ├── pyimod02_archive.pyc
│       │   ├── pyimod03_importers.pyc
│       │   └── struct.pyo
│       ├── out00-Analysis.toc
│       ├── out00-EXE.toc
│       ├── out00-PKG.pkg
│       ├── out00-PKG.toc
│       ├── out00-PYZ.pyz
│       ├── out00-PYZ.toc
│       ├── out00-Tree.toc
│       ├── out01-Tree.toc
│       └── warnpyCalc.txt
├── dist
│   └── pyCalc
├── pyCalc.py
└── pyCalc.spec

4 directories, 17 files
tasdik@Acer:~/Desktop/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:

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

Till then. Goodbye!

References: