Python to .exe Executable.
Tags: python, executable, automation, application
Published: 2021-03-21 10:57:35

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
```
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](https://nsis.sourceforge.io/Download)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqtxa5iaz8lyozv4vxcz.PNG)
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](https://github.com/pratik6217/Python_Games/blob/master/Guess_the_number.py)

Now open a terminal in the current directory by pressing Shift + Right mouse Button and selecting open a command window here.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h05a55atce5dhrdb1nib.png)
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"
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jze3k0szkx59s1bbewjf.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1tiw30lnn8y8z9yuhcm.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5fmpsfcs5lwt9lidcx5.PNG)

Now, go ahead and open The NSIS Software. It will look somewhat like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xaj2an66g3pdf123ekrh.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dno4ugqnp3p4x93evur5.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qk720d6dnqcn6lrk8hgc.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0tiqze1aiwu2eqgg6lu.png)