DEV Community

Cover image for Python to .exe Executable.
Pratik Mishra
Pratik Mishra

Posted on • Updated on

Python to .exe Executable.

As we all know at some point in our coding journey we always wanted to compile our pyhton code into an executable file so that we can simply run it by just double clicking it !!
I mean who doesn't wants to have an executable file of their code and boast about it in front of our friends( just joking )

So, Let's begin with it
First of all you'll need two things One is pyinstaller which you can easily install by typing in:

pip install pyinstaller
Enter fullscreen mode Exit fullscreen mode

and the second thing is NSIS which you can download it from their official website.
Just Download them and keep it ready and don't worry I'll walk you through each and every step one by one.
Here is the link for the NSIS Download Page:
NSIS

Once everything is installed and ready to go we can move forward.
Navigate on to your folder where the python file is.
For E.g:
Alt Text
Here is a simple guess the number game which I made in Python.
If you want to see the code for the same you can checkout here:
Guess The Number

Now open a terminal in the current directory by pressing Shift + Right mouse Button and selecting open a command window here.
Alt Text
or you can simply type cmd in the file explorer section.

Now once a terminal is open in the current dirctory go ahead and type this:

# Type the name of the file without the quotes.
pyinstaller --onefile "Name of your python file.py"
Enter fullscreen mode Exit fullscreen mode

The --onefile flag compiles all the files into a single executable.
Like if you have various dependancies and other files which is required for your program then it will all be compiled into a single file.
Also there is another flag known as -w use this flag if you don't need a terminal in your program say if you have GUI then you can use this flag. Since mine is a simple command line program I didn't use it.

It will take some time to compile your code depending upon the size and the dependencies once it has completed you will have multiple folders created just like this:
Alt Text

Now go to the dist folder you'll have your executable file there just move that file to the main folder where you have all your code and dependencies and that's it you can just double click it and run your code as an executable just like an application.
Alt Text

Now, moving further you can even compile all this into an installer by using NSIS as follows:
first make a zip of the folder where you have all the files and the exectable file which you just made just like this:
Alt Text

Now, go ahead and open The NSIS Software. It will look somewhat like this:
Alt Text

Select the Installer based on .zip file and the select the zip file which you made just now and then click on generate in the bottom right corner:
Alt Text
This will create an installer which you can easliy share with your friends who can just download it run it and boom they have all the file pre compiled and ready to go !!
Alt Text

This pretty much makes your task easier to share your programs and folders to anyone also it makes it beginner friendly for those who are new to programming.

Alt Text

Top comments (2)

Collapse
 
daniel_leiva profile image
Daniel Leiva

Are you trying to use a venv to install packages for the project?