r/learnpython • • Oct 30 '26

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

28 comments sorted by

7

u/Lumethys Oct 30 '26

there's no point in doing so

0

u/lichen_13 Oct 30 '26

Les logiciels propriétaires ne le font pas?

6

u/randomman10032 Oct 30 '26

Tbh the only way thats reliable is to make it available on some webserver.

1

u/lichen_13 Oct 30 '26

What do you mean ? Like the code is only available when a user is connecting to the server?

1

u/Rhomboid Oct 30 '26

No, the only code you can secure is code that only runs on your machines (server only.) As soon as it's running on a person's individual machine, the entire game is up and there's nothing you can do. There are decades of evidence that if the code runs on your computer, you can reverse engineer it and there's no prevention.

1

u/TheRNGuy Nov 01 '26

Via something like JSON (JSON data will be free, but what it does on server, no one will know)

-4

u/Jazzlike_Platypus430 Oct 30 '26

No, only when they login to the server 😂

2

u/lichen_13 Oct 30 '26

This does not very respond to my question

-1

u/Jazzlike_Platypus430 Oct 30 '26

It does respond to Like the code is only available when a user is connecting to the server? but you have to understand it or prove me wrong.

6

u/Gnaxe Oct 30 '26

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.

0

u/lichen_13 Oct 30 '26

D’accord je comprends concernant le open source le code que j’ai créé reste le mien?

1

u/Gnaxe Oct 30 '26

I am not a lawyer. Copyright law isn't completely universal, but there are treaties. If you wrote it yourself, yes, you automatically get copyright, but it's up to you to enforce your claims. You might not even know a violation exists. And if what you wrote was derived from something else, or written with AI assistance, or it was too trivial to be copyrightable, then it may not be yours, or not entirely. If you own the copyright, you can publish under an open-source license if you want. That's irrevocable, but you're still the copyright holder, so you can publish again under other terms. However, once you accept contributions, you may not own the copyright to those.

3

u/jackbrux Oct 30 '26

You can minify/obfuscate it. But what is sensitive inside the code? Are you storing anything like API keys? You shouldn't do that

0

u/lichen_13 Oct 30 '26

I am actually storing a webhook

2

u/jackbrux Oct 30 '26

Is it supposed to be private? Anyone will be able to see that if know how, even if you obfuscate

4

u/realmauer01 Oct 30 '26

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 Oct 30 '26

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 Oct 30 '26

The only REAL way would be to make it a web app with a library like Django, FastAPI or Flask.

1

u/babarock Oct 30 '26

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 Oct 30 '26

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 Oct 30 '26

Share and share alike ...

2

u/lichen_13 Oct 30 '26

What do you mean?

1

u/[deleted] Oct 30 '26

[removed] — view removed comment

1

u/zaphodikus Oct 30 '26

Nuitka is really easy to use, but it depends more on how much money is left on the table. If the answer is none, then stick with what you got.