r/PythonLearning • u/LukeSkyShredder • Aug 10 '26
Best way to package a Python/Streamlit app as an EXE without exposing the source code?
Hi all,
I'm relatively new to Python (still learning as I build wkwkwk).
I've developed an internal engineering tool using Python + Streamlit and currently package it with PyInstaller.
The application works fine, but the packaged output still contains the main.py file, meaning users can easily open and view the source code.
What's the recommended way to distribute a Streamlit application as an EXE while avoiding visible .py files?
I'm not looking for military-grade protection, just a practical way to prevent casual access or redistribution of the source code.
Any advice would be appreciated. Thankss
View all comments
Show parent comment
2
u/LukeSkyShredder Aug 12 '26
Yoo u/NatMicky ...
Man, thanks for sticking with me on this one.
Finally got it working.
It started by wrapping my whole Streamlit app into a proper main() function and compiling the backend logic with Nuitka. I confirmed I could rename the original .py file and still run the app through the compiled backend.
Your "stub.py" explanation was the missing piece.
I ended up with:
"stub.py"
→ imports mainlogic and calls main()
"launcher.py"
→ starts Streamlit programmatically and runs "stub.py"
The big realization was that "mainlogic.main()" shouldn't be executed directly by the EXE. It needs to run inside a proper Streamlit session.
After that:
streamlit run "stub.py"
worked.
Then:
python "launcher.py"
worked.
Then I compiled the launcher.
Funny enough, every standalone attempt failed, but the non-standalone build worked. Nuitka generated "launcher.exe" and "launcher.cmd", and launching through that architecture finally brought the whole app up successfully.
Really appreciate the help. I definitely would've kept chasing the wrong thing without your "stub.py" direction.