In this article, we are going to learn how to convert Python to exe file. We all know that it’s very simple to create GUI applications with Python like using the Tkinter library in Python. But, what if you want to share your application with someone who doesn’t know how to run Python. The answer is that you will ask him to watch online blogs for setting-up Python in their system and then run it.
But wait, we have very simple 3 methods using which you can convert your Python application to exe files and share them directly with anyone. So, let’s start to learn how to convert Python to exe file.
Methods to Convert py to exe file
- Using pyinstaller
- Using auto-py-to-exe
- Using cx_Freeze
1. Using pyinstaller to compile Python to exe
Basic Overview of PyInstaller tutorial
In this first method to convert py to exe file, a Python program and all of its dependencies are combined into one package by PyInstaller. The user does not need to install any modules or a Python interpreter in order to launch the packed program. By introspecting your Python code, identifying your dependencies, and then packaging them into the appropriate format based on your Operating System, PyInstaller accomplishes this astounding accomplishment.
There are many fascinating subtleties about PyInstaller, but for the time being, we’ll only cover the fundamentals of how it functions and how to utilize it. Additionally, PyInstaller may produce Windows, Linux, or macOS executables. Accordingly, Linux users will receive a standard executable, Mac users a bundle of .app files, and Windows users a .exe.
Steps
Step 1: Installation of pyinstaller using the following command:
pip install pyinstaller
Step 2: Now open the folder where your py file is stored and open that folder in terminal
There are other ways of doing this but the best one is to open your folder in the terminal. This will help you to not worry about copying and pasting the path where your script is stored. Here in our case, our attendance_management.py file is stored in a folder named attendance_management. So we opened that folder. Once that folder is opened, right-click and select the option Open in Terminal.
Output:
Step 3: Now paste the below command in your Terminal/Windows Power Shell
pyinstaller --onefile -w 'filename.py'
Now once you paste this command into your terminal and hit enter, a message will be displayed in your terminal notifying you that the exe file is created. Please note that you need to replace ‘filename.py’ with your file name (python file) that you want to convert to exe.
Step 4: Check your folder
Once you run that command, come back to your folder and you will see that there are two new folders namely build and dist, and one spec file is created. You might get confused by seeing these folders that where is the exe file. You can find your exe file in the dist folder. The explanation of each folder is further explained along with their purpose.
Output:
Step 5: Open the dist folder and there’s your exe file
Now once you open this dist folder you will see the exe file that you can share with others without the need for source code. But you might have questions that why there is a build folder and one spec file and what are they responsible for. As far as these folders are concerned then they are of not that much use to beginners but if you want advanced settings then you can modify them accordingly and use it. A brief description of created folders is as below:
- dist folder: The last artefact you should distribute to your users is located in the dist/ subdirectory. You can find a file with the name of your entry point inside the dist folder. As a result, your executable file in this example will be called attendance_management.exe.
- build folder: The majority of the metadata and internal recordkeeping for creating your executable is stored by PyInstaller in the build subdirectory. The build folder can be helpful for troubleshooting, but unless there are issues, you can generally ignore this folder.
- spec file: Knowing what to concentrate on first is crucial. Specifically, the executable you can make available to your users and any potential debugging data. The pyinstaller command by default will produce the following useful items:
Finally, we have converted our py file to exe using python pyinstaller. This was the very first method to create exe from python file. Although this method is quite simply the best one is yet to come. Go on reading and find out which one is best for you.
2. Using auto-py-to-exe to create exe from python
Basic Overview for auto-py-to-exe
Another easy and unique method to convert python to exe file is a Python utility called Auto-py-to-exe which can easily transform a Python.py file into an executable that has all of its dependencies packed. Another advantage is that Auto-py-to-exe creates an executable file that is a built version of the source code rather than the original source code. This makes it harder for others to steal your code.
Here, in this section of the article, “convert python to exe,” we’ll go over all there is to know about auto-py-to-exe, from how to install it to how to use it.
Steps
Step 1: Installation of auto-py-to-exe using the below command:
pip install auto-py-to-exe
Step 2: Now to run auto-py-to-exe, use the following command:
auto-py-to-exe
Once you run the above command, a GUI will appear on your screen and that will help you to convert the py file to exe. This GUI offers many options like selecting the path, options like one directory or one file are our application windows or console-based, etc. We will go through each option in detail. The output will be as shown below:
Output:
Step 3: Pick the .py file that you want to convert to .exe
Here we will convert our Attendance Management System project python file to exe file. In this step basically, you have to select the file that you want to convert using the Browse button. The path in the blue box shows the path of your selected file.
Output:
Step 4: What to choose One File or One Directory?
One File: If “One File” is selected, “Auto PY to EXE” will produce a single .exe file that contains all dependencies but NOT media files. The following explanation can be skipped if your software solely uses the standard Windows GUI and doesn’t have any icons, backdrops, or media files, or if you’re fine with placing a media folder close to an executable file. You should read Step 5 if you want to pack media files directly into a .exe file.
One Directory: The “Auto PY to EXE” option, when set to “One Directory,” will place all dependencies in a single location. The “Advanced” menu’s “Output directory” option is available. If you put media files or folders in the Output directory, using media assets like icons and backdrops inside your .exe shouldn’t be a problem.
Step 5: Select Additional Files
You can add whatever files you want using the “Additional Files” menu in “Auto PY to EXE.” But there’s a catch. Pyinstaller is used by “Auto PY to EXE” to unpack data into a temporary folder and store the directory path in the _MEIPASS environment variable. Because the path has changed, your project won’t be able to locate the required files and won’t recognize the new path either. In other words, the selected files from the “Additional Files” menu will not be included in the .exe file if the option “One File” is selected.
Step 6: Run your program by clicking on CONVERT .PY TO .EXE
Now once you hit on that button, you might see some processes happening in the Output section. Once your file is converted (might take some time based on the size of your project) you will see two buttons at the bottom and a message as shown in the Red box will display a completion message with a path where your .exe file is saved. By clicking on the OPEN OUTPUT FOLDER, it will take you to the folder where your exe file is saved and another button CLEAR OUTPUT will let you select another project that you want to convert.
Output:
3. Using Python cx_Freeze for py2exe
Basic Overview of python cx_freeze
This article’s last and third approach to Convert the py script to an exe file is quite intriguing. There are a few choices, and there is a considerable amount of interest in converting to .exe for distribution. Py2exe is a fantastic option for Python 2.7, while cx freeze performs admirably with Python 3.
Similar to how pyinstaller and auto-py-to-exe work, cx_Freeze is a collection of scripts and modules for turning Python scripts into executables. In contrast to these two utilities, cx Freeze is cross-platform and ought to function on any system that Python is compatible with. It works with Python 2.7 or later (including Python 3).
Steps
Step 1: We must first obtain cx Freeze. Download it from this page or set up pip to do it:
pip install cx-Freeze
Step 2: Now you have to create a setup.p file in the same folder where your main file is.
For example, We have a main folder named Attendance Management System and in that, we have the attendance_management.py file, so we created a setup.py in our main folder.
Now in that setup.py file paste the following code:
import sys from cx_Freeze import setup, Executable # Dependencies are automatically detected, but it might need fine tuning. # "packages": ["os"] is used as example only build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]} # base="Win32GUI" should be used only for Windows GUI app base = None if sys.platform == "win32": base = "Win32GUI" setup( name="guifoo", version="0.1", description="My GUI application!", options={"build_exe": build_exe_options}, executables=[Executable("attendance_management.py", base=base)], )
Explanation:
Line 1 to 2: Here we have imported sys and cx_Freeze library
Line 6: This option automatically takes care of all the required packages
Line 9 to 11: These three lines basically make sure that which version of windows you are using 32 bit or 64 and on the basis of that it changes the base variable
Line 13 to 19: This part holds the description of our project. Here you can replace the description field value with what you want and in the executables variable, you have to provide the main python file that you want to convert. In our case it is attendance_management.py
Output:
Step 3: Now simply use the below command to build your executable file
python setup.py build
Explanation:
This line will simply make a build folder in your main folder and in that folder your executable file will be there. Click on that exe file and you are ready to share your project as software with your peers. Here’s the end. You have finally learned for creating exe from python file.
Output:
Reference Links
Conclusion
Hurray!! Cheers if you have made it to the end. In this article on “compile python to exe file”, we have covered the latest and most efficient methods to convert python file to exe file. All these three methods are reliable but we encourage you to go for auto-py-to-exe as it is fast and easy to use by even a novice programmer. We hope this article was up to your expectation and we promise to bring new articles every day. Till then go on learning, executing, and building new projects.
Thank you for visiting our website.
Also Read:
- Free Python Certification course from Alison: Good for Resume
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- Top 5 Websites to Learn Programming in 2024
- Python Internship for college students and freshers: Apply Here
- New secrets to Earn money with Python in 2024
- Connect with HR Directly – Job Hack
- Google offering free Python course: Enroll Today
- Google Internship 2024
- TCS Launched Free Certification Course with Industry Recognized Value
- What is web development for beginners?
- Microsoft Giving Free Machine Learning Course: Enroll Now
- Accenture Giving Free Developer Certificate in 2023
- Python | Asking the user for input until they give a valid response
- Amazon Summer Internship 2023
- Amazon Giving Free Machine Learning Course with Certificate: Enroll Now
- Google Summer Internship 2023
- 5 Secret ChatGPT skills to make money
- Free Google Certification Courses
- 5 AI tools for coders better than ChatGPT
- New secrets to Earn money with Python in 2023
- How to utilize ChatGPT to improve your coding skills?
- Create your own ChatGPT with Python
- Bakery Management System in Python | Class 12 Project
- SQLite | CRUD Operations in Python
- Event Management System Project in Python
- Ticket Booking and Management in Python
- Radha Krishna using Python Turtle
- Hostel Management System Project in Python
- Sales Management System Project in Python
- Bank Management System Project in C++