Picture this: you’ve just finished creating a fantastic Python program and intend to let the world see it. You then send your friend a directory containing all your scripts and encourage them to try it out. Only first, they must install Python and then run the program via the IDLE shell or the command line. But your friend isn’t willing to use the command line.
Or, you’re an Administrator responsible for getting all team members in the organization to run your Python script, including the non-technical staff. How will you distribute your code to other users so they only have to click on an executable file?
Fortunately, numerous Python utilities can help you turn .py file into .exe by “freezing” your code and building an executable file. This guide will take you through the three most popular approaches and tools. These solutions are straightforward and versatile, but you may prefer one over the others, depending on your use case.
Don’t wait, download now and transform your career!
Your FREE Guide to Become a Data Scientist
Don’t wait, download now and transform your career!
Different Ways to Turn a .py File into a .exe File
Here are the three most common strategies that you can use to distribute Python files to users who expect executable files:
This Python module effectively bundles your script alongside each dependency into one executable known as myscript (myscript.exe). It lets you turn the file into the application, allowing the user to run it without being forced to install Python packages.
Most users prefer it because it delivers something they understand – a single executable to launch. However, you must prepare to distribute any related files (such as README) separately.
To use this module, you must first install it via the following command: pip install.py installer.
Once you’ve successfully installed it, direct your command prompt to the specific folder that contains your Python script, then type this command: py installer –onefile filename.
If it’s a virtual environment, ensure you install it again on the infrastructure with the same command. As confusing as it may sound, installing Pyinstaller on the base and virtual environment is necessary.
If you run the command, it will begin converting the Python file into exe. It will create three directories, and the one with the name dist contains your .exe file. So navigate to it, open the project folder, and then click on the .exe file.
At first, the application may not be as practical as expected, so ensure you paste all dependencies such as text files and images inside the project folder.
When you run the program, you’ll notice that it’s functioning as a standalone application. The main benefit of this approach is that it’s executable even on systems that don’t have Python. You can download Pyinstaller here.
CREATING AN .EXE FILE WITH AUTO-PY-TO-EXE
This second method of converting .py files into .exe offers an incredible Graphical User Interface (GUI) that addresses everything necessary to facilitate the conversion.
You’ll begin by installing the latest auto-py-to-exe version by simply opening a terminal then running the pip install auto-py-to-exe command. You must ensure that the working environment where you intend to install the Python package contains the necessary libraries to run your script.
After installation, you’ll create an executable file by writing the auto-py-to-exe command. After you’ve run the command, a GUI application will open, and you’ll use the following steps to turn a .py file into an executable file.
Add The Location of the Script
Browse the Python script you intend to convert, then augment it to Script Location. You can choose any script. But if your script has to read a path, ensure you leverage absolute paths. This is because relative paths may not behave as you would expect with .exe files.
You may have to include this line of code: application_path = os.path.dirname(sys.executable). This will help you find out where the .exe file is situated and change your script appropriately to read or export files to their respective directories.
Choose “One File” or “One Directory”
Choosing “Window Based” or “Console Based”
The third step involves deciding whether to display the console or not. The “Console-Based” option will open the console after running the .exe file, a recommended approach if your script creates console-based outputs. But if you don’t want the console outputs to display when running the .exe file, go for “Window-Based.”
You may also use the advanced option that lets you add an icon, the necessary files for running your script, and more.
If you see the error “ModuleFoundNotError” after double-clicking the .exe file, you’ll begin from the first step, but now using the “Advanced” option.
Convert the File
You’ll conclude the process of converting the .py file to an executable application by clicking on the blue-colored “Convert” button.
It’s crucial that auto-py-to-exe displays the code that.py installer needs to deliver an .exe file behind the fancy GUI displayed on the screen. This should be right above the Convert button. Once you’ve completed the process, your executable file should be right within the output directory you picked in the fourth step. Go here to download auto-py-to-exe.
Converting Using py2exe
Finally, you can avoid the numerous prerequisites of executing Python scripts using the.py 2exe module. It works like the other modules, but it’s ideal for Python Three, and you can’t rely on it in Python Two.
You’ll begin by installing the module using this line of code: pip install.py 2exe. You’ll then come up with the setup.py file inside your target folder, then open your command prompt and type setup.py.py 2exe command.
When you run the module, it’ll create a dist folder containing all the files. Make sure you add you replace script.py with your preferred file name.
The last stage of this process involves adding any important libraries and programs to the setup. You can download py2exe here.
The Bottom Line
It totally makes sense to send ready-to-view applications to other users if you want them to peak and share their insights. Fortunately, you can convert a .py file into a .exe file using the above processes.