In this Python tutorial, we will see, step by step how to convert Python file to exe using Pyinstaller. If you want to convert python file to exe, then this article will be very much helpful to you.
Convert python file to exe using pyinstaller
Recently, we have created an application that generates experience letters for the employees who are leaving the organization. For this, we have used the Python Tkinter to create the form. You can check out, Create Word Document in Python Tkinter. You can get the complete source code also
We have created the application but we cannot deliver it because the client is unaware of python and other programming-related jargon. It won’t be easy for them to create-activate the environment and then run the app each time.
So we have decided to give them a setup file of the program that can be installed on the system. In this tutorial, we will talk about converting the python to exe file and then creating a setup that can be installed.
Convert Python file to exe windows
Pyinstaller is a python module using which the python file (.py) can be converted to an executable file (.exe).
- Install the pyinstaller directly on the default environment using the below command.
pip install pyinstaller
- In case of virtual environment install the pyinstaller again on that the virtual environment using the same command.
- This may sound little confusing but it is important to install pyinstaller on both base environment and virtual environment.
- We assume that you have prepared the application folder as suggested in our blog – Create word file using python tkinter.
- Follow the below command to convert the python (.py) to executable (.exe) in python.
# conversion with cmd console pyinstaller filename.py # conversion without cmd console pyinstaller --noconsole filename.py
- Once the file process is completed you will see new folders created. Navigate to dist folder -> project folder -> click on application (.exe) file.
- At first, application may not work as expected, paste all the dependencies like images, text files, etc. inside that project folder.
- Run the program and you will see that program is running as a standalone application. Even systems with no python will be able to execute this program.
- Incase you are unable to follow instructions given so far please follow the same instructions below with images.
Follow the steps to create exe file from python (.py) file [in detail]
The below image shows the files of the application to generate an experience letter. To see how we created this application visit our blog – Create Word File Using Python Tkinter.
Activate the virtual environment and install pyinstaller in it. It is mandatory to install pyinstaller on the universal environment and virtual environment. Otherwise, your applications won’t able to access the modules installed in virtual environment.
Using pyinstaller you can convert the python file to exe. Type pyinstaller <python-file-name>, this will convert the .py to .exe file with console. Add –no-console keyword after pyinstaller to convert python file to executable without command prompt.
After executing the above command, you will few new folders have been created in the same directory. These folders have information about the conversion, open the dist folder to access the converted file.
Inside dist folder, there is another folder with the name of that py file. Suppose, at the time of conversion the python file name was main.py then this folder name will be main.
In the next section, we will use this folder to create a setup file using NSIS tool.
Inside the main folder, you will see all the files and dependencies used in this application. There you will see a .exe file with the same name as the python file was. Add the files and output folder in this main folder before executing the program.
Create a installable package using NSIS tool
This section is all about the NSIS tool using this we can create a bundle up all the dependencies under one file.
- Download the software from the above mentioned link and start the software.
- Convert the folder that we have prepared in the previous section into zip file.
- On the NSIS tool click on the Installer based on .ZIP file, from the prompt select the zip from your computer and click on generate button at the bottom-left.
- Wait for some time and once completed close the software, navigate to the folder you will see a disk shaped file.
- Your file is ready, you can share it with others.
- If this summary of steps didn’t helped you then below we have explained each step in detail with images.
python create setup file [Step by Step]
In the above section, we have mentioned the main folder. This folder will be used to create the setup file for the application – experience letter generator in Python.
If you have followed the above section, that means you have added files and output folders inside this main folder. Now convert them into a zip file.
Open NSIS software, click on the ‘Installer based on .ZIP file’ -> In the prompt select the zip file by clicking on the open button and after selecting the zip file click on the generate button. We have marked the sequence on each step.
After clicking on the generate button, it will take some time to create the installable setup file. Once completed you will see the window as below. Close the window by clicking on the close button.
Congratulations on successfully converting the python (.py) file to an executable (.exe). And the packaging of the executable into an installable setup file. In the below image, the marked image shows the setup file. You can install this file in your system.
With this, we have concluded our project of converting a python file into an executable setup. The file is ready to be shared with friends, family, and clients.
Related Python tutorials:
- Python Tkinter Validation
- Python Tkinter On-Off Switch
- Python Tkinter Multiple Windows Tutorial
- Python Tkinter Animation
- Python Tkinter Table Tutorial
We hope this tutorial, have helped you in converting python file to exe. This is how to convert Python file to exe using Pyinstaller. In case you are facing any issues(s) please write us with the exact error message.
A Machine Learning Engineer with 4 years of IT Experience. I have worked in Multiple Domains and my sole aim is to simplify the tech. Currently working with Tsinfo Technologies as a Python developer, and by profession, I am Programmer, Blogger, and YouTuber on my way to make this world a better place.