How to convert Streamlit app into .exe Executable file?

Step by step tutorial with demo for sharing your Streamlit application

Mehul Gupta
Data Science in your pocket
3 min readJul 4, 2024

--

Recently, I got a tricky problem statement at my hands i.e. to develop a Data Science POC with a UI.

What’s tricky in that?

Just pickup either Gradio or Streamlit and build a minimal UI for your project. No?

Even I thought the same. But the tricky part was this POC has to be shared with some Non-tech banking folks as they need to have a first hand experience of the app. To run any python based application, be it Streamlit or Gradio based, you would need a number of installations that can’t be done for the consumers of this app in my case.

Checkout the full running demo here

One of the solutions is to deploy this application on some server and let the non-tech folks play with it but deployments are time taking and as this was just a POC, noone wants to take this much of pain as in any MNC, deployments may take some months to get done.

The only solution I was left with was to somehow convert this Streamlit application into an .exe file which can be shared with them and the Banking guys don’t need to install anything.

Just double click and the app is available.

This post is about how I converted my Streamlit app into the desired .exe file. We will be doing this using cx_Freeze.

1.Create a virtual env

Trust me, this is a must else you might land yourself in trouble. Creating a virtual env is required as

  • When you will be converting your app into exe, all the dependencies present in the current environment will get wrapped in the .exe. So, if you have installed 100s of libraries, everything will get wrapped and your .exe will become huge.

My 1st attempted .exe was 33GBs big

  • Also, there may be many discrepancies between packages and hence the .exe may break down. The lesser the dependancies wrapped, the better

Use the below code to create a virtualenv right away

pip install virtualenv

virtualenv test

#to activate test
.\test\Scripts\activate

2.Install required libraries

Don’t forget to install all the libraries you wish to use. Also, use

streamlit==1.13.0

altair=4.0.0 (a dependency of Streamlit)

If you wish an error free life

3.Using cx_Freeze

We would be using cx_Freeze for creating the .exe in our case

Note: Every other tutorial on the internet uses pyinstaller. I tried pyinstaller and many other packages for about a week with no success. Also, I assume cx_Freeze is an easier alternate as well with minimal work to be done.

  1. First of all, pip install cx_Freeze and other required files for your streamlit app, including streamlit
pip install cx_Freeze streamlit==1.13.0

2. Create a wrapper file for your Streamlit main file

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


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("streamlit_app.py"),
"--global.developmentMode=false",
]
sys.exit(stcli.main())
  • When you want to bundle your Streamlit app into an executable file (.exe) for distribution, this wrapper script helps set up the environment and commands correctly.
  • This wrapper files helps in calling your streamlit main file (assume streamlit_app.py in this case) programmatically. Do remember to change the name !
  • Do import all your required libraries for your project in this file ( as I’ve imported pandas) to avoid dependency discrepancy while creating the .exe even if they aren’t getting used in this file.
  • Name this file run.py and keep both ‘streamlit_app.py’ & ‘run.py’ in the same folder say ‘test’.

3. Run the below command

cxFreeze --script run.py

This will run for some minutes and will create a folder ‘build’

4. Copy-paste ‘streamlit_app.py’ and other required files (say CSV you might be using ) used in the project in ‘build/run/your_OS/’. Here, a file with the name ‘run.exe’ would also be present alongside some other dependency files

5. Double click on run.exe

Your .exe is ready

For sharing purposes, zip the entire ‘test’ folder and guide the non-tech guys to navigate to run.exe.

--

--