Using PyInstaller (or similar) to create an executable

try copypasting the st_on_hover_tabs folder to place where .exe is created.

Hello everyone,

So I tried this method for the executable but I encountered a problem when I want to launch the executable. When I just the terminal to launch it I have no problem it launch [on server.port 8501] but when I double click on it, it deployed on server port 3000 telling me:

2023-05-12 18:12:39.928 DEBUG   streamlit.web.server.server: Starting server...
2023-05-12 18:12:39.929 DEBUG   streamlit.web.server.server: Serving static content from the Node dev server
2023-05-12 18:12:39.937 DEBUG   streamlit.web.server.server: Server started on port 8501
2023-05-12 18:12:39.938 DEBUG   streamlit.runtime.runtime: Runtime state: RuntimeState.INITIAL -> RuntimeState.NO_SESSIONS_CONNECTED
2023-05-12 18:12:40.212 DEBUG   git.cmd: Popen(['git', 'version'], cwd=/Users/c.bajan, universal_newlines=False, shell=None, istream=None)
2023-05-12 18:12:40.250 DEBUG   git.cmd: Popen(['git', 'version'], cwd=/Users/c.bajan, universal_newlines=False, shell=None, istream=None)

I even check with: lsof -ti tcp:8501 | xargs kill to be sure to have nothing on this port but no change.
Does someone else encountered this issue and tell me what to do?

@charlesmyu Thanks a lot it works like a charm with streamlit 1.22 and pyinstaller 5.10 .
@littletuna Thanks too for the import, that’s indeed an odd behaviour.

But do you have a problem with session.state ? since the app is first read in python and not with streamlit, we can get this error:

2023-05-23 09:26:43.640 WARNING streamlit.runtime.state.session_state_proxy: Session state does not function when running a script without `streamlit run`

Any work around this ? Execept using Global variables ?

I just replied to post on Stack Exchange about bundling a Streamlit app into an executable. I know this feature request is linked way, way up in this topic, but I feel like it’s very underrated given how often I see this issue.

Anyone who wants Streamlit to be easier to package into a self-contained executable, please vote on this feature request so the devs can track its popularity.

Feel free to comment about your use case/scenario that makes it a desirable feature for you over some local server deployment. If the devs know people want it (and why they want it) that will increase the likelihood of it getting attention. :grin:

1 Like

@charlesmyu thanks for the info, it really saved so much time for the newer versions of streamlit

However, I’m getting problems with streamlit 1.22, pyinstaller 5.11. If my app doesn’t use st.table, I’m fine, but if I use st.table, I’m stuck.

It works when run with python, but when I run pyinstaller, it complains of missing hidden import “pyarrow.compat”. which is fine until I use st.table, at which point it throws exception:

Traceback (most recent call last):
  File "pyarrow\pandas-shim.pxi", line 100, in pyarrow.lib._PandasAPIShim._check_import
  File "pyarrow\pandas-shim.pxi", line 56, in pyarrow.lib._PandasAPIShim._import_pandas
ModuleNotFoundError: No module named 'pyarrow.vendored.version'
Exception ignored in: 'pyarrow.lib._PandasAPIShim._have_pandas_internal'
Traceback (most recent call last):
  File "pyarrow\pandas-shim.pxi", line 100, in pyarrow.lib._PandasAPIShim._check_import
  File "pyarrow\pandas-shim.pxi", line 56, in pyarrow.lib._PandasAPIShim._import_pandas
ModuleNotFoundError: No module named 'pyarrow.vendored.version'
2023-05-30 14:06:54.031 Uncaught app exception
Traceback (most recent call last):
  File "streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
  File "C:\_JIRA\Transcriber\dist\Transcribe.py", line 89, in <module>
    st.table(setData)
  File "streamlit\runtime\metrics_util.py", line 332, in wrapped_func
  File "streamlit\elements\dataframe_selector.py", line 156, in table
  File "streamlit\runtime\metrics_util.py", line 332, in wrapped_func
  File "streamlit\elements\arrow.py", line 159, in _arrow_table
  File "streamlit\elements\arrow.py", line 197, in marshall
  File "streamlit\type_util.py", line 757, in data_frame_to_bytes
  File "pyarrow\table.pxi", line 3681, in pyarrow.lib.Table.from_pandas
  File "pyarrow\pandas_compat.py", line 570, in dataframe_to_arrays
  File "pyarrow\pandas_compat.py", line 385, in _get_columns_to_convert
  File "pyarrow\pandas-shim.pxi", line 126, in pyarrow.lib._PandasAPIShim.pd.__get__
  File "pyarrow\pandas-shim.pxi", line 96, in pyarrow.lib._PandasAPIShim._check_import
  File "pyarrow\pandas-shim.pxi", line 56, in pyarrow.lib._PandasAPIShim._import_pandas
ModuleNotFoundError: No module named 'pyarrow.vendored.version'

Any advice would be appreciated. Meanwhile, I’m going to convert it to a markdown table so it would work, but would much prefer st.table

I have seen yours.But can use ‘etna’ library.
I have created exe file of streamlite using npm,pyodide. But my use is specific using ‘etna’ library thats available in PYPI but not in npm.
Can you try this please.
telegram:@bk_itmo

Hi! Thanks for the step by step guide. I followed all the steps, but I am getting this error:

“FileNotFoundError: [WinError 3] The system cannot find the path specified: ‘C:\Users\HP\AppData\Local\Temp\_MEI164442\jsonschema_specifications\schemas’
[5940] Failed to execute script ‘run_app’ due to unhandled exception!”

I have folders that start with _MEI followed by all sorts of numbers, but no folder called _MEI164442, which is obviously what this error is referring to. Would appreciate if you could help. Thanks.

Hi @Steve_Liu, I was having the same issue. I managed to solve it by adding tuple (“.streamlit_venv/Lib/site-packages/pyarrow/vendored”, “./pyarrow/vendored”) to ‘datas’ list in run_spec file:

datas = [(“${YOURPYTHONENV}/Lib/site-packages/altair/vegalite/v5/schema/vega-lite-schema.json”,
“./altair/vegalite/v5/schema/”),
(“${YOURPYTHONENV}/Lib/site-packages/streamlit/static”,
“./streamlit/static”),
(“${YOURPYTHONENV}/Lib/site-packages/streamlit/runtime”,
“./streamlit/runtime”),
(“${YOURPYTHONENV}/Lib/site-packages/pyarrow/vendored”,
“./pyarrow/vendored”)
]

Hope this helps

Need for help. My desktop take a lot of time to execute pyinstaller --onefile --additional-hooks-dir=./hooks run_main.spec --clean. Why ?

Dear Charlesmyu,

thank you for your helpful post on creating a Streamlit .exe file for a simple “Hello, World” application. It was immensely useful. However, I’ve encountered an issue when attempting to create an .exe file for a more complex project, and I received the following error message:

Traceback (most recent call last):
File “”, line 198, in run_module_as_main
File “”, line 88, in run_code
File "C:\Users\703321944\Projects\Text Inquisition\application_test\app-beta\a02Aug2013.ticketEinstein\Scripts\pyinstaller.exe_main
.py", line 7, in
File "C:\Users\703321944\Projects\Text Inquisition\application_test\app-beta\a02Aug2013.ticketEinstein\Lib\site-packages\PyInstaller_main
.py", line 194, in console_script_run
run()
File "C:\Users\703321944\Projects\Text Inquisition\application_test\app-beta\a02Aug2013.ticketEinstein\Lib\site-packages\PyInstaller_main
.py", line 180, in run
run_build(pyi_config, spec_file, **vars(args))
File “C:\Users\703321944\Projects\Text Inquisition\application_test\app-beta\a02Aug2013.ticketEinstein\Lib\site-packages\PyInstaller_main_.py”, line 61, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File “C:\Users\703321944\Projects\Text Inquisition\application_test\app-beta\a02Aug2013.ticketEinstein\Lib\site-packages\PyInstaller\building\build_main.py”, line 1019, in main
build(specfile, distpath, workpath, clean_build)
File “C:\Users\703321944\Projects\Text Inquisition\application_test\app-beta\a02Aug2013.ticketEinstein\Lib\site-packages\PyInstaller\building\build_main.py”, line 944, in build
exec(code, spec_namespace)
File “ticketEinstein.spec”, line 54, in
coll = COLLECT(
^^^^^^^^
File “C:\Users\703321944\Projects\Text Inquisition\application_test\app-beta\a02Aug2013.ticketEinstein\Lib\site-packages\PyInstaller\building\api.py”, line 931, in init
self.postinit()
File “C:\Users\703321944\Projects\Text Inquisition\application_test\app-beta\a02Aug2013.ticketEinstein\Lib\site-packages\PyInstaller\building\datastruct.py”, line 184, in postinit
self.assemble()
File “C:\Users\703321944\Projects\Text Inquisition\application_test\app-beta\a02Aug2013.ticketEinstein\Lib\site-packages\PyInstaller\building\api.py”, line 994, in assemble
shutil.copy2(src_name, dest_path) # Use copy2 to (attempt to) preserve metadata
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\703321944\AppData\Local\anaconda3\Lib\shutil.py”, line 436, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File “C:\Users\703321944\AppData\Local\anaconda3\Lib\shutil.py”, line 258, in copyfile
with open(dst, ‘wb’) as fdst:
^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: ‘C:\Users\703321944\Projects\Text Inquisition\application_test\app-beta\a02Aug2013\dist\ticketEinstein\a02Aug2013\.ticketEinstein\Lib\site-packages\imblearn\under_sampling\_prototype_selection\tests\pycache\test_repeated_edited_nearest_neighbours.cpython-311.pyc’

test_repeated_edited_nearest_neighbours.cpython-311.pyc’ is avaliable in the pyenv can you sugest how to resolve this.?

I have a problem about the importing module in my app.py file. I used the following method to generate my app (.exe), but i find app.py lacks import libraries (importing modules). I don’t why.

I import the following modules in app.py:
"
import glob
import streamlit as st
import wget
from PIL import Image
import torch
import cv2
import os
import time
import onnxruntime
from utils import *
from utils.general import *
from ultralytics.utils.plotting import Annotator, colors, save_one_box
from utils.augmentations import letterbox
import re
"

I get the following error when i run the exe file:
2023-09-20 17:10:04.156 Uncaught app exception
Traceback (most recent call last):
File “streamlit\runtime\scriptrunner\script_runner.py”, line 552, in _run_script
exec(code, module.dict)
File “D:\8_py_project\flange_detect_deploy\dist\app.py”, line 3, in
import wget
ModuleNotFoundError: No module named ‘wget’
2023-09-20 17:10:36.335 Uncaught app exception
Traceback (most recent call last):
File “streamlit\runtime\scriptrunner\script_runner.py”, line 552, in _run_script
exec(code, module.dict)
File “D:\8_py_project\flange_detect_deploy\dist\app.py”, line 3, in
import wget
ModuleNotFoundError: No module named ‘wget’

i am getting below error after running run.exe.
importlib_metadata.PackageNotFoundError: No package metadata was found for streamlit
[11700] Failed to execute script ‘run’ due to unhandled exception!

Could someone please help me to resolve this issue?

i am getting below error after running run.exe.
importlib_metadata.PackageNotFoundError: No package metadata was found for streamlit
[11700] Failed to execute script ‘run’ due to unhandled exception!

Could you please help me to resolve this issue?

Hey Guys,

I was working on this stuff today and came across several issues that many of you have encountered. Thanks to the community for providing solutions. Here to lend some helping hands and share what I’ve learned to help resolve these common problems.

Version Update for Altair/Vega-Lite :

  • For the new version of Altair/Vega-Lite, update the reference to use v5 instead of v4.

Error with Magic Functions & Import Issues :

  • When using Streamlit with PyInstaller, you might encounter errors with magic functions and importing modules.
  • This is because only Streamlit metadata is loaded, not the entire package. This issue is noted by charles, hmas, kevin, and nikhil.
  • Solution: Modify hook-streamlit.py as follows:
from PyInstaller.utils.hooks import copy_metadata, collect_submodules
datas = copy_metadata('streamlit')
hiddenimports = collect_submodules('streamlit')

PyArrow Issue for @Steve_Liu

  • PyArrow Error:
    • The use of pyarrow with PyInstaller’s public hook can lead to errors.
    • Solution: Create a new file named hook-pyarrow.py in the hooks directory with the following code:
from PyInstaller.utils.hooks import collect_submodules, collect_data_files
hiddenimports = collect_submodules('pyarrow')
datas = collect_data_files('pyarrow', include_py_files=True)

Dkdatascientist

  • Custom Hook Recommendation:
    • It’s advised to use a custom hook instead of PyInstaller’s default for specific packages for your case
    • FYI: If PyInstaller has a public hook for a package, it will use it; otherwise, it will fall back to a custom hook.

for yuchang_zhu ,

  • Adding Hidden Imports:
    • To include necessary imports for Streamlit, add the following to your .spec file:

hiddenimports=['streamlit', 'wget']

Finally Streamlit Hook for @mohamed_zekkiriya ,

  • Streamlit Hook Setup:
    • As discussed by hmasdev and for clarity to @mohamed_zekkiriya, set up your working directory as follows:

WORKINGDIR/
- hooks/hook-streamlit.py
- main.py
- run_main.py

In hook-streamlit.py , use this code:


from PyInstaller.utils.hooks import copy_metadata, collect_submodules
datas = copy_metadata('streamlit')
hiddenimports = collect_submodules('streamlit')

Cheers lads (Wish everyone could get notified , Since I am new user I can only tag two people :upside_down_face: :melting_face:)

you save my life and my job.
You are my daddy.

The solution works generally with Pyinstaller. However I am facing always issues like “ModuleNotFoundError” when starting the executable file. How can I ensure that all required packages are installed? Where can I insert that it installs a “requirement.txt” file?

Thanks for answering!

please, a tutorial project example for a hello world app executable. Thanks!

1 Like
1 Like

For streamlit_folium use “from streamlit_folium import folium_static” and in run_main.spec:

(
            ".../LocalCache/local-packages/Python310/site-packages/streamlit_folium/frontend/build",
            "./streamlit_folium/frontend/build"
        )
1 Like

Hey, I am using the pyinstaller solution, specifying the port 8501 in both flags and .toml and the CMD that opens says port 3000. When I open in google the port 3000 I get: this site can´t be reached and when I open 8501 I get 404 not found error. Any clues on how to fix it?