Converting a python script (.py) to an executable (.exe)

So you’ve written your first python script or program to simplify your life, automate a task, or some machine learning. How do you distribute it to the rest of the world? Sorry, but only nerds use python.


Enter the pyinstaller library. This handy little python library will take your python script, all its dependencies, and wrap them all up in a neat clean executable. Follow the two steps (ONLY TWO!) below to install pyinstaller and run the script to convert your python script into a Windows executable (.exe).


Step 1: Install pyinstall. Open a command window, and run the following command.

  • In Windows 10, click the search button, type command prompt, and open the command window.
  • In Linux, open your favorite terminal. Enter the command below
pip install pyinstaller

Step 2: Run pyinstaller on your python script.

  • If you closed your command window or terminal, open a new one.
  • Use the ‘cd’ command to navigate to your python file in the command or terminal window.
  • Type the code below to run pyinstaller and generate a single executable for your script.
  • You can drag and drop your .py file into the command window to include the full file path if you did not change directories to where your .py script is located.
pyinstaller -Fw filename.py

pyinstaller will work its magic and in just a few minutes you’ll have your very only windows executable (.exe). The -F option tells pyinstaller to create a single executable file. The -w option tells it to NOT create a command terminal when running your program.

Step 3 (Optional): Open the “dist” folder the pyinstaller made to find your .exe. Test your executable to ensure it works before sending it out to the world!


Okay, okay, so there are really three steps if you want to be a stickler. But hey, not bad right? Now you’ve got your very own Windows executable (.exe) from a python file (.py) you can send to most anyone in the world to run! How about that!

John

Leave a Reply

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