Mar 5, 2023 2 min read

Turn Python code into EXE - using pyinstaller

Turn Python code into EXE - using pyinstaller
Table of Contents

I show how to generate a requirements.txt (essential to Python projects), and how to generate a single exe file from a Python project using "pyinstaller."

TL;DR - I show how to generate a requirements.txt (essential to Python projects), and how to generate a single exe file from a Python project using "pyinstaller."

fuzzyListDedupeGUI

The below GUI is from a project I used Tkinter and ChatGPT on check it out 😁.
https://blog.cybersader.com/fuzzy-deduplicate-bullet-points-from-chatgpt/

fuzzyListDedupeGUI - Github

How to Generate an EXE from the Python Code

Using PyInstaller — PyInstaller 5.8.0 documentation

  1. Navigate to your project directory in the terminal using the cd command - navigate to wherever your Python code and virtual environment are.
  2. Use the pipreqs library to generate your requirements.txt file.

    This is essential when creating Python projects so that other people can replicate the attached libraries and dependencies. In the case of my program, the only dependency is the "fuzzywuzzy" library which will show up as fuzzywuzzy==0.18.0 in the requirements.txt file

    To use piqreqs use the following commands while in the same directory as your Python file:
    pip install pipreqs
    pipreqs . --force

    This will scan the Python files in the current directory and generate a requirements.txt file that includes all the necessary dependencies.

    The --force option will overwrite the existing requirements.txt file if one already exists.

    Note: The dot . indicates the current directory. You can also specify a path to a specific directory if your Python files are located elsewhere.
  3. Use PyInstaller to create the EXE file. Run the following commands in your CLI:

    pip install pyinstaller
    — THEN RUN —
    pyinstaller --noconsole --onefile --paths=[path to the folder with your dependencies in it] main.py

     If you're using a virtual environment then the path to the "lib" (libraries/dependency code) folder will be used in the command similar to this: pyinstall --noconsole --onefile --path=./venv/Libs/site-packages main.py . This is how I did it with the code generated using Pycharm as my IDE/editor.

    I use the --noconsole flag because I didn't want a console to pop up when I run the exe by double-clicking it.

Note on Virtual Environment Issues in Pycharm

If I ever get the "activate.ps1" error, I just do the following in Pycharm:

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Cybersader.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.