In the Python ecosystem, we are spoiled for choice when it comes to libraries and frameworks that aid in crafting beautiful applications. However, if you’ve ever tried sharing your amazing Python app with a friend, you know the headache of making sure they have the correct Python version, the right dependencies, and getting everything to run properly.
That’s where PyInstaller comes in handy. In this post, we’ll walk through turning your Python project into an executable file that you can share with others, even if they don’t have Python installed. We’ll also cover adding this executable to the PATH environment variable for easy access.
Table of contents
Open Table of contents
What is an Executable
An executable file (.exe
), is a type of computer file that performs specific actions according to encoded instructions when it’s opened or “executed”.
To understand this in simpler terms, let’s consider a real-world analogy. Imagine you have a cooking recipe (like a Python script), and you’re the chef (the Python interpreter). To prepare the dish, you need to read the recipe and follow the instructions step by-step. This requires knowledge of cooking, time, and ingredients (dependencies).
Now, consider an instant meal from a store. This instant meal is like an executable file. It’s already prepared. You don’t need the recipe, cooking knowledge, or ingredients. All you have to do is heat it (execute it), and it’s ready to serve.
Similarly, an executable file is a pre-compiled program, bundled with everything it needs to run. When you double-click or run it from the command line, the computer follows the instructions packed into the file. This allows the program to function without any external dependencies or need for an interpreter to read and execute the code.
It’s the easiest way for users to run a program: just click or run the file, and it does its thing!
PyInstaller: The Magic Wand
PyInstaller is a fantastic tool that packages your Python applications into standalone executables. The users of your app don’t need to install Python or any dependencies; they can simply run the executable.
Let’s walk through converting our Python project into an executable.
Installing PyInstaller
You can install PyInstaller via pip:
pip install pyinstaller
Creating an Executable
To convert your Python script into an executable, navigate to your script’s directory in the terminal and run:
pyinstaller --onefile yourscript.py
Replace yourscript.py
with the name of your script. The --onefile
option tells PyInstaller to package everything into a single executable file.
Once the process completes, you’ll find your shiny new executable in the dist
folder that PyInstaller creates.
Renaming the Executable
To give your executable a specific name, use the -n
or --name
option:
pyinstaller --onefile -n myapp yourscript.py
This will create an executable named myapp
.
Adding the Executable to the PATH
Next, we’ll add our executable to the system’s PATH environment variable. Doing so allows us to run the program from any location on the command line, just by typing its name.
For Windows
- Navigate to the location of your
.exe
file in File Explorer. - Click on the address bar, copy the full path.
- Press
Win + X
and chooseSystem
. - Click on
Advanced system settings
. - In the
System Properties
window, on theAdvanced
tab, clickEnvironment Variables...
. - In the
Environment Variables
window, scroll down in theSystem variables
section, select thePath
variable, then clickEdit...
. - Click
New
and paste the path you copied. - Click
OK
in each window to close them.
For UNIX-like systems (Linux, MacOS)
- Open a terminal.
- Navigate to the directory containing your executable.
- Add the path to your executable to the PATH variable in your shell’s configuration file (like
.bashrc
or.zshrc
), e.g.,export PATH=$PATH:/path/to/your/exe
. - Source your shell configuration file (
source ~/.bashrc
or equivalent) or restart your terminal.
And voila! You can now run your program by typing its name in the terminal, no matter which directory you’re in.
Why Go to All This Trouble?
Packaging your Python applications into executables and adding them to the PATH offers several benefits:
- Ease of Use: Users can run your application with a simple command, without needing to know how to navigate directories in the terminal.
- No Dependencies: Your executable includes its own Python interpreter and any necessary libraries, so users don’t need to install anything else.
- Distribution: It’s easier to distribute a single executable file than a bunch of
.py
files and arequirements.txt
file. - Versatility: Your application can be used in shell scripts and other programs, just like any other command-line tool.
Wrapping Up
By creating an executable of your Python project, you simplify the process of sharing your application with others. PyInstaller and the PATH environment variable are two powerful tools that make Python applications more accessible, shareable, and user-friendly.
Happy coding!
References
- PyInstaller docs: https://pyinstaller.org/en/stable/