Last Updated on July 14, 2022 by Jay
Do you know that you can “freeze” your python code and give other people run it even if they don’t have Python installed? They can just run the script like any program/app on a computer (Windows, Mac, or Linux). No Python, no library installation required.
At some point you might want to give your Python script to others to run on their machines. However, one common problem is that not everyone knows Python or have Python installed. “Freezing” refers to a process of creating a single executable file by bundling the Python Interpreter, the code and all its dependencies together. The end result is a file that others can run without installing Python interpreter or any modules.
Freeze Your Python Code With Pyinstaller
Pyinstaller is a Python library that can freeze Python scripts for you and it’s very easy to use. According to its official description:
PyInstaller reads a Python script written by you. It analyzes your code to discover every other module and library your script needs in order to execute. Then it collects copies of all those files – including the active Python interpreter! – and puts them with your script in a single folder, or optionally in a single executable file.PyInstaller official site
We’ll need to download pyinstaller via pip:
pip install pyinstaller
Note that the latest pyinstaller is compatible with Python 3.6-3.9, which are more recent versions. If you are using an earlier version of Python, I highly recommend you upgrade to at least 3.8 and it’s also for your benefit.
Let’s recycle a previous example… yes I’m lazy 🙂
I want to focus on the pyinstaller so we’ll recycle the previous groupby example with the following code:
import pandas as pd
df = pd.read_csv(r'E:\PythonInOffice\freeze_python_code\cc_statement.csv', parse_dates=['Transaction Date'])
df['Transaction Date'] = pd.to_datetime(df['Transaction Date'])
df['Day'] = df['Transaction Date'].dt.day_name()
df['Month'] = df['Transaction Date'].dt.month
spend_category = df.groupby('Category').agg(Total_spend=('Debit','sum'),
We basically try to analyze our credit card statement and generate a summary table with total and average spending for each category. The data source is a mocked-up credit card statement with 533 transactions throughout year 2020. The output is a dataframe with only 14 rows and 2 columns.
Freeze! Our Code
Now we are ready to freeze our code!
In the command prompt window, type
pyinstaller freeze_eg.py. “freeze_eg.py” is the script that you want to freeze, so you have to change it to whatever your file name is.
After a moment when the freezing is done, you will see several new folders in your working directory, go into “dist” folder and find the application file with the same name as the script, in my case “freeze_eg”.
Double click to run the app, you will see that the app just generated an Excel file named “spend_by_category.xlsx” in your working directory.
Everything we need to run the app is within the “dist” folder so we can delete other extra folders and files, and we can just send the dist folder to another person and they’ll be able to run our Python app. However, the folder still contains tons of files and it could be confusing for others which file to run. Conveniently, pyinstaller provides some really cool command line input arguments we can play with.
- To freeze Python code into a single file, use the
- To add an icon to our app, use the
Now head back to the “dist” folder, and we have a beautiful and professional looking Python app that can be run almost on any computer, with or without Python installed.