Convert your Python file to executable windows file

Hi mates, In this post we are going to learn how to convert your python files(apps,games,animations etc.) to an executable windows file, so that you can distribute it on web or to your friend and family without asking them to separately download python or any library for python.

There are many methods to do this including Py2exe , cx_Freeze, Pyinstaller and some other. they all do pretty much the same thing they freeze(or compile) your python file into executable file(or a single package).

We are going to use Pyinstaller , because it is very easy to use ,comes with different feature, it is compatible with 3rd party packages and also supports most of the python libraries(including PyQt, Django and matplotlib).

I am going to use the python file made in my last post on Sorting algorithm animation using Pygame .
I made a little change so that animation doesn't stop after sorting is completed ,instead it starts again with new random array.You can download the file here to get started or you can use your own.

Below are the steps to use Pyinstaller ....

STEP-1 : Installing Pyinstaller on system :

Assuming that you have python and required libraries of of your files installed on your computer the easiest way to install Pyinstaller is pip. open your command window by right click on start button or Windows+r then type cmd .Now in command window type:

pip install pyinstaller

and wait for command window to say it's completed.

STEP-2 : Converting your .py files to .exe files :

Now make a folder and put your file in that folder(or you can use your previous folder). Select the folder (in which the .py file is) hold Shift and press Right mouse click and select open command promp here(or open powershell here).

Now in command window(or Powershell) type :

pyinstaller <filename.py>

replace <filename.py> with your python file name. I am using the file with name sorting.py. so i would type :

pyinstaller sorting.py

And that's it . Just wait for the process to be completed .
When it gets completed , go back to the folder in which .py file is ,there will be some new files and directories you have to open the folder named dist , In this folder you will find you have your executable file with same name as before you can run it.

Note : make sure your command window is running in same folder in which the .py file is located . if not try using cd command to locate to that folder:

cd <address of folder>

ADDITIONAL STEP : Different options (hide console,add icon,make a single file)

I => If you don't want a console to pop up every time you run your program. you can do that by using -w command :

pyinstaller -w sorting.py

II => If you don't want a all those messy files and want just single file to consist your whole program you can do that by using -F command(note that it's capital F):

pyinstaller -F sorting.py

III => and if you want to add a custom icon to you app(or game) , you can use -i command :

pyinstaller -i <icon address> sorting.py

you can either use the address of icon file or you can paste the icon in same folder with .py file and then use just it's name instead. I am using sort.ico and i have placed it in same folder with sorting.py

pyinstaller -i sort.ico sorting.py

IV => you can can use any combination of upper codes or all of them at one time. like this:

pyinstaller -w -F -i sort.ico sorting.py

It will create a single file with icon sort.ico and no console will pop-up upon running the app.

Thanx  for reading this article.For any query or suggestions comment below.
And if you liked this post please share the post and subscribe to this blog.
And also don't forget to like our facebook page to get updated : www.facebook.com/TheCreatricks

Comments

Popular posts from this blog

5 Easy steps for beginners to make animation of sorting algorithms using Pygame

Animation of Sorting Algorithms