If you want to give a Python application to another person who doesn’t have the python interpreter on their computers, you have to create an executable (exe) file. After you do this, you can send them only a single file, and the application will work correctly.
In this tutorial, I’ll explain how to convert Python scripts to executable files using different .py to .exe converters. This will work on Windows, Linux, and Mac.
If you don’t have pip installed, you can do it from the terminal or Pycharm. Check the post about Python and PIP installation.
There are a few .py to .exe converters, like pyinstaller, py2exe, py2app, and cx_freeze.
In this tutorial, I’ll show you the one I use, namely pyinstaller.
First, open the run window by using the Windows button + R, type “cmd” press Enter.
This will execute the command line.
Type pip install pyinstaller and hit enter to start the installation.
When that’s done, type PI installer and hit enter in the same window. If there is no error, it has been installed correctly.
Now, go to the folder where your Python script is located, by using Shift + right-click. This will give you new options: Open command window here or Open PowerShell window here.
First, I’ll show you the basic way to compile a file, and then how you can disable a console and how to add an icon.
Create an exe file with a console
Create a file called myscript.py with the following code.
text = input("Enter the file name: ")
myfile = open(text, "w+")
Type pyinstaller myscript.py in the command line and hit enter. It will take a short time to compile and you will get three new folders and an additional file.
After it’s finished, look in the folder to find the executable file you’ve created. Go to dist >> myscript and execute the myscript.exe file. A window with a question will appear. Enter the name of the file to create and press enter.
As you can see, there is a new text file called myfile.txt. It has to be located in this folder, otherwise, it won’t work.
Create exe file without a console
If you don’t want the console to appear, add a -w between pyinstaller and your file pyinstaller -w myscript.py to disable the console.
Create a script with the following code.
myfile = open("myfile.txt", "w+")
Now, when you run the executable, a console will not appear, and the text file will be created.
Create a single exe file
If you want all generated files in one executable file, type pyinstaller -F myscript.py. When you look at the files generated, there are 3 directories. Go to the dist >> myscript directory and there will be only one file – myscript.exe. It’s bigger than before, but you can take it outside the directory and it will work.
Add an icon
A cool feature of pyinstaller is that you can add an icon. Use the -i flag to add an icon followed by the icon file. The file must be in an .ico format, otherwise, the operation will fail. It has to be in the same directory as the script.
If you don’t have an icon file, you can use this converter.
Type pyinstaller -i D:\myscript\icon.ico myscript.py. Now, when you look at the executable file generated, it will have your chosen icon.
Combine all three methods
You can combine these flags however you want. I generally use them all by typing pyinstaller -w -F – i icon.ico myscript.py.
It will generate one executable with no console, and the icon attached to it. It can be found in the dist directory.