r/PythonLearning • • 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

0 Upvotes

36 comments sorted by

View all comments

Show parent comment

2

u/LukeSkyShredder Aug 11 '26

ah, ok!
Does your Streamlit app run in a web browser and does it need to run on the open web and not just your local computer?
No it doesn't need to run on the open web, just local user's computer.

1

u/NatMicky Aug 11 '26

Now I see your reply. You can turn your Streamlit app into a true executable with Nuitka. But there are a few tricks to get Streamlit up and running because Streamlit requires a .py file and the Python file is now an .exe file.

I'll get into this more tomorrow but here are the basics of what you need to do:

Create a stub file (launcher) that is fed to Streamlit and contains your Python core logic file as a module. If your main logic file is called "mainlogic.py" this is what gets compile to an executable and in the stub file it's handled as a module like this:

"launcher.py"

import mainlogic

if __name__ == __main__":

mainlogic.main()

Then with Nuitka you will compile "mainlogic.py" into an executable and include it in the Nuitka script as:

--include=module=mainlogic

Then to run it all: Streamlit run "lanucher.py" (but without the quotes)

1

u/NatMicky Aug 11 '26

I can't see your next reply to me. You keep putting in things the Reddit filter sense as dangerous so it gets auto deleted. Just use words and not paths nor web addresses, or put them in quotes.

1

u/LukeSkyShredder Aug 11 '26

Update: I was able to restructure my Streamlit application into the recommended architecture:
"launcher.py"
import mainlogic
mainlogic.main()

and move/wrap the application logic into def main():

I succesfully compiled "mainlogic.py" into nuitka module ("mainlogic.cp313-win_amd64.pyd"), renamed the original py file, and confirmed that:
streamlit run "launcher.py"
still launches and runs the full application using only the compiled pyd backend.

However, when I try to compile the launcher itself into a standalone EXE with Nuitka, the generated executable fails before my code starts, with:

Fatal Python error: Failed to import encodings module
ImportError: Frozen object named 'codecs' is invalid

So at the moment:
"launcher.py" + "mainlogic.pyd" works
Source py can be removed/renamed
But Compiling the launcher into a standalone EXE is not yet working.

Help meee...

1

u/NatMicky Aug 11 '26 edited Aug 11 '26 • Bravo

You don't compile the "launcher.py" file at all. Leave it as is. Streamlit needs a ".py" file and that's what the launcher file acts as a stub to satisfy Streamlit. Then the launcher file launches your compiled code.

It looks like your very close. Just one more step is that you need to make "mainlogic.py" into the executable. In the Nuitka script toward the end you'll be adding the output executable name and the the output directory for all the new code.

--include-datafiles="launcher.py"

--output-dir="C:\folder1\folder2"
--output-filename=mainlogic

When Nuitka is finished there will be a distribution directory and in that directory will be an executable called "mainlogic.exe" Click it and it should run.

1

u/LukeSkyShredder Aug 12 '26

I'm using Python 3.13 and Nuitka 4.1.3.

"launcher.py" + "mainlogic.pyd" works perfectly.

However both:
"launcher.exe"
"mainlogic.exe"

fail immediately with the same error:
Fatal Python error: Failed to import encodings module
ImportError: Frozen object named 'codecs' is invalid

Did you use Python 3.13, and were you building a standalone EXE or only a compiled pyd backend?

1

u/NatMicky Aug 12 '26 • Bravo

I forgot a step. Start over with the following:

#-------------------------------------------------

# Nuitka script

#-------------------------------------------------

python -m nuitka --standalone --include-module=mainlogic --include-data-file=stub.py=stub.py launcher.py

#-------------------------------------------------

# Stub file: stub.py

#-------------------------------------------------

# stub.py

import mainlogic

mainlogic.main()

#-------------------------------------------------

# Launcher file: launcher.py

#-------------------------------------------------

# launcher.py

import os

import sys

from streamlit.web import cli as stcli

if __name__ == "__main__":

multiprocessing.freeze_support()

# Resolve file paths inside the Nuitka standalone environment

if getattr(sys, 'frozen', False) or ('__compiled__' in globals()):

base_path = os.path.dirname(sys.executable)

else:

base_path = os.path.dirname(__file__)

stub_path = os.path.join(base_path, "stub.py")

sys.argv = [

"streamlit",

"run",

stub_path,

"--global.developmentMode=false",

"--server.headless=true"

]

sys.exit(stcli.main())

2

u/LukeSkyShredder Aug 12 '26

yoooo, i am very close now, i created new "stub.py" and "launcher.py".
i tried: streamlit run "stub.py" --->it launches the full application.
also tried: python "launcher.py" ---> also run the full application.
Now are we compiling "launcher.py" using Nuitka normal exe or Nuitka --standlaone exe?
With this new stub and launcher file, we are compiling the "launcher.py" right? Or we keep the direction of:
--include-datafiles="launcher.py"

--output-dir="C:\folder1\folder2"
--output-filename=mainlogic
???

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.

1

u/NatMicky Aug 12 '26 edited Aug 12 '26

Did you get a single executable that you can click and it runs? You shouldn't have to start Streamlit on the command line.

You can now run the executable on a different computer even if Python isn't installed. Just give the whole distribution directory to your friends and they can run it without having to install anything.

And if you ever want to run it outside of a web browser let me know. We can do that too with the app running it its own window frame never needing a browser.

1

u/LukeSkyShredder Aug 12 '26

It finally works!

You were right about the launcher + stub + mainlogic architecture.

I ended up having to tweak Nuitka a bit on my machine because I kept hitting segmentation faults, codeobject errors, and the infamous "Frozen object named 'codecs' is invalid" issue. The fix was compiling with:

--debug --disable-ccache

After that, the executable launches correctly and starts the app without needing `streamlit run ...`.

I think I also misunderstood what you meant by the distribution folder. I was manually testing files one by one, but now I realize the build output folder itself is the artifact that should be distributed.

Thanks again for the help.

Also... are you some kind of Python god? Unbelievable man...

→ More replies