Skip to content

Standalone

Prerequisites

Before we get started, make sure you have the following libraries:

pip install cx_Freeze  
pip install streamlit

cx_Freeze creates standalone executables from Python scripts, with the same performance, is cross-platform and should work on any platform that Python itself works on.

Creating a wrapper code

The first step is to create a wrapper code to run your main application. Besides all the code shown below, be aware to import all the libraries that your app needs to run on this file, so that all of them will be compiled by cx_freeze.

For this tutorial, let’s call this wrapper run.py and your Streamlit app script file main.py. This code was offered by ploomber.io, check them out.

── your_app_folder/  
│ ├──run.py  
│ ├──main.py
import streamlit  

import streamlit.web.cli as stcli  
import os, sys  

# Import the other libraries you need here  

def resolve_path(path):  
    resolved_path = os.path.abspath(os.path.join(os.getcwd(), path))  
    return resolved_path  

if __name__ == "__main__":  
    sys.argv = [  
        "streamlit",  
        "run",  
        resolve_path("main.py"),  
        "--global.developmentMode=false",  
    ]  
    sys.exit(stcli.main())

Now, we can use cx_freeze to create the executable file. Run the following command in the terminal.

cxfreeze -c run.py

Once the wrapper will run our interface, keep in mind that this solution requires main.py inside the cx_freeze regenerated folder.

── cx_freeze_generated_folder/  
│ ├──lib  
│ ├──share  
│ ├──app.exe  
│ ├──**main.py**  
│ ├──other_files(images,streamlit .py pages, etc..)

The advantage of this approach is that, if no library is added during your code reviews, you can edit your interface without the need to compile the wrapper again.

Covering all the bases

In some cases, cxfreeze might face some problems to deliver pyc to one or more libraries in your dependency import. Pyc files are compiled bytecode files that are generated by the Python interpreter when a Python script is imported or executed.

Let's suppose cxfreeze could not generate a pyc file in the boto3 library. To get trough this issue, make sure you follow these steps:

Find the missing pyc in your python interpreter files. If you’re using an env in conda, you can find it here:

C:\Users\<user>\Miniconda3\envs\<your_env_name>\Lib\site-packages\boto3

After you find where the pyc files are located, copy them to the cxfreeze generated files, following the subfolder where they were located:

── cx_freeze_generated_folder/  
│ ├──lib  
│ │  ├── boto3  
│ │    ├──**sub_folder/**  
│ ├──share  
│ ├──app.exe  
│ ├──main.py  
│ ├──other_files(images,streamlit .py pages, etc..)

In other scenarios, cx_freeze might fail with different file types or even not deliver a whole library folder. To resolve this issue, find the lib folder in your conda and copy it to the cxfreeze folder we’ve just build.

── cx_freeze_generated_folder/  
│ ├──lib  
│ │  ├── "**copy lib files here"**  
│ ├──share  
│ ├──app.exe  
│ ├──main.py  
│ ├──other_files(images,streamlit .py pages, etc..)

Additional Tips

  • If your Streamlit app relies on external data files or assets, make sure to include them in the same directory as the executable or specify their paths correctly in your code.
  • Keep in mind that creating a standalone executable can result in a larger file size compared to running the app directly through Streamlit.
  • Test your standalone executable on different platforms to ensure it works as expected.
  • You can start customizing your executable with Resource Hacker, like changing its icon to a more exclusive experience.
Last Updated: 2025-01-13

Comments