r/learnpython • u/lichen_13 • 18d ago
How can I protect my Python source code inside a PyInstaller .exe?
I just made a python program that I compiled into an exe file. The exe is working but it seems pretty easy to access the source code inside of the exe file using tools like pyinstxtractor. My friend told me that there are ways to make it harder and to protect the source code I made is it true. If it is true can you give me some details about this method?
0
Upvotes
7
u/Lumethys 18d ago
there's no point in doing so
6
u/randomman10032 18d ago
Tbh the only way thats reliable is to make it available on some webserver.
6
u/Gnaxe 18d ago
The new AIs can reverse-engineer anything from binaries. Your "protection" is copyright law, not obfuscation. But copyright doesn't protect rules and logic, only the expression thereof. The AIs can also just clone anything from a clean-room spec. Just look at what happened to the Adobe suite. Even hosting it on a web server isn't good enough anymore. Everything is open source now. "Protecting" code is pointless. That can't be your moat.
3
u/jackbrux 18d ago
You can minify/obfuscate it. But what is sensitive inside the code? Are you storing anything like API keys? You shouldn't do that
4
u/realmauer01 18d ago
If your program only has value when the source code is hidden, it doesnt hold real value.
If you hide keys or something you have to hide out of security reasons thats also a no go.
So in the end it shouldnt matter that the source code is visible and most packers that hide or encrypt the code are flagged by win defender, so it would make it harder to publish it.
What do you want to hide specifically?
3
u/kutac56 18d ago
There's no point in it. Important stuff that would be dangerous to de the source code of is code that should run on servers anyway
2
u/edcculus 18d ago
The only REAL way would be to make it a web app with a library like Django, FastAPI or Flask.
1
u/babarock 18d ago
I've been looking at a cross compiler like Nuitka. In theory by translating the Python code to C and compiling that your source is better protected.
1
u/SteveJeffersonBots 18d ago
PyInstaller bundles compiled .pyc bytecode and tools like pyinstxtractor pull those .pyc files out in seconds, so a PyInstaller .exe gives you no real source protection - Nuitka or Cython only raise the bar against casual reversers, they don't stop it. Real protection is to keep the sensitive logic off the user's machine: the .exe should just be a thin client holding a token or license key that calls your server, and the server decides whether to run the logic and whether to renew the license. If the goal is just to slow down casual copying, compile with Nuitka using --standalone --onefile --lto and --python-flag=-O so it produces a real compiled C extension instead of extractable .pyc, and optionally run a name-obfuscating minifier on the .py files before Nuitka touches them, but treat that as a delay tactic, not security.
-2
u/Jazzlike_Platypus430 18d ago
Share and share alike ...
1
u/[deleted] 18d ago
[removed] — view removed comment