How to convert a Python file to exe using pyinstaller?

PyInstaller is a Python script that creates Windows .exe files from Python programs. There are Several ways to turn your Python (.py) program into an executable (.exe) with the required libraries and packages. However, the PyInstaller package installer converts the Python (.py) file to an executable (.exe) file. It is quite a common issue faced by developers. They may want to create an application as an executable program.

There are the following steps followed to convert a Python (.py) file into an executable (.exe) file.

  • Install PyInstaller library into your Window OS directory 
    • python project file(.py) that we interested to convert to executable(.exe) file
      • Save the Python Project with .py extection
  • command line to convert .py file to .exe file
  • upon compilation a new folder dict contains .exe file
  • Executable (.exe) file testing

What is PyInstaller in a Python package? 

PyInstaller is a Python package that allows you to compile your Python applications into executables (.exe). It is used to create a distribution of your application for deployment on multiple operating systems. It contains a library and tools used to build an installer compatible with various platforms and environments using the package manager pip

Why is there a need to convert a .py file into a .exe file?

There are several grounds why you may need to convert a Python file into an executable.

  • The Windows OS does not support Python scripts in any other way (until and unless Python PowerShell installed), so this is one reason to run them from the command line from within Windows. For example, if you want to execute the Python file within Windows, then the only way you can do this is by converting your .py files into .exe files.
  • Another reason is if other users on your computer system do not have Python installed on their machines and you want to share your Python scripts to them. The other user runs the Python (.Py) file on their system within Window without opening the Python Environment to execute the file. 

Steps to convert a Python file to exe using pyinstaller

However, to convert a .py (Python) file to an executable (.exe) file in Python goes through the following steps. Follow these steps to accomplish this task. 

Install the PyInstaller library into your Windows OS directory 

To install the PyInstallerinstaller package into your Python directory, you need to install the package manager pip into your environment. 

However, check for the package installer pip already installed. If it is already installed it shows the following message;

However, if it is not installed it will display the following error message;

'pip' is not recognized as an internal or external command, operable program or batch file.

However, to install pip in your window environment, write the following syntax into your command prompt;

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Now you download the get-pip.py. Lastly install the pip package into your window environment by putting the using the following command into your command prompt; 

python get-pip.py

After successful installation of pip package installer, you can check the version of the installed pip package.

 pip --version

Here is the installed pip package version.

Now you are ready to get into pyinstaller 

Install the pyinstaller package into your Python directory using the following command line to convert Python (.py) file to executable (.exe) file.

pip install pyinstaller
Collecting pyinstallerNote: you may need to restart the kernel to use updated packages.

  Using cached pyinstaller-5.7.0-py3-none-win_amd64.whl (1.3 MB)
Collecting pefile>=2022.5.30
  Using cached pefile-2022.5.30-py3-none-any.whl
Requirement already satisfied: pywin32-ctypes>=0.2.0 in d:\data_science\setup\lib\site-packages (from pyinstaller) (0.2.0)
Collecting altgraph
  Using cached altgraph-0.17.3-py2.py3-none-any.whl (21 kB)
Requirement already satisfied: setuptools>=42.0.0 in d:\data_science\setup\lib\site-packages (from pyinstaller) (58.0.4)
Collecting pyinstaller-hooks-contrib>=2021.4
  Using cached pyinstaller_hooks_contrib-2022.15-py2.py3-none-any.whl (253 kB)
Requirement already satisfied: future in d:\data_science\setup\lib\site-packages (from pefile>=2022.5.30->pyinstaller) (0.18.2)
Installing collected packages: pyinstaller-hooks-contrib, pefile, altgraph, pyinstaller
Successfully installed altgraph-0.17.3 pefile-2022.5.30 pyinstaller-5.7.0 pyinstaller-hooks-contrib-2022.15

You can run the above command into your Window terminal as well.

python project file(.py) that we interested to convert to executable(.exe) file

The following is the Python (.py) file that we are interested to convert to the executable(.exe) file

#import  tkinter from the library
from tkinter import *
#create a class named tagged as 'SmileyFace'
class SmileyFace:
    #define a distrctor within class to initialize object state
   # 'self' is the class instance itself
#within the methos pass one argument. here it is canvas
    def __init__(self, frame):
        #using dot(.) operator to assess the method argument canvas
        self.frame = frame

        frame.create_oval(70, 70, 350, 350, fill='red')
        frame.create_oval(125, 125, 175, 175, fill='yellow', tags='left')
        frame.create_oval(225, 125, 275, 175, fill='yellow', tags='right')
        frame.create_line(125, 250, 275, 250, width=5, tags='mouth')

    def smile(self):
        self.frame.delete('right||mouth')
        self.frame.create_oval(225, 125, 275, 175, fill='red', tags='right')
        self.frame.create_arc(125, 225, 275, 275, extent=-180, width=5, fill='yellow', tags='mouth')
def main():
    win = Tk()
    frame = Canvas(win, width=400, height=400)
    frame.pack()

    smiley = SmileyFace(frame)

    Button(win, text='Smile', command=smiley.smile).pack()
    Button(win, text='Quit', command=win.destroy).pack()

    win.mainloop()

main()

Save the Python Project with .py extection

Run the above code in your Jupyter/Python Script environment. This will create a (.Py) extension file. However, download this Python file into the same folder where your Python environment is installed. 

command line to convert .py file to .exe file

Now after installing pyinstaller, execute the following command into your window terminal.

pyinstaller <filename.py>

Here in the following example;

pyinstaller face.py

However, the above command will execute and generate a SPEC, PY and executable (.exe) file in the dict folder by creating a new folder.

I am working on the Anaconda Environment, and I’ve installed my setup in local Disk (C:)

However, saved your Python (.py) file into the same folder as Anaconda environment is.

a new folder dict will contains .exe file

When I execute the above command the dict ‘new folder’ will generate in the same folder where anaconda setup is installed. 

Now open the dist folder.

You will find the folder is created for the same Python file (face.py in our case). However, the new folder in dict folder, is acquired the same name as the Python file has (face.py). 

Open the new folder created in the dict file, you’ll find the executable(.exe) file of the saved Python (.py) file.

However, in this way you will convert the .py file to the .exe file.  

However, Right click on the above executable file, to explore its properties.

Upon conversion, your Python file will appear with the .exe extension. 

Executable file testing

Now it’s time to check if the executable is working or not. However, click on it or run the .exe file with the administrator.  Here is the output generated.

Conclusion

This tutorial demonstrates step-by-step instruction to convert a Python (.py) file to an executable (.exe) file using pyinstaller. However, the need to convert .py file to .exe file is also discussed in detail here. Follow the instructions and create the executable (.exe extension) file of your own Python Project.

Leave a Comment

Your email address will not be published. Required fields are marked *