"Is there a good way to handle this problem?" No. Nothing can be protected against reverse engineering. Even the firmware on DVD machines has been reverse engineered and the AACS Encryption key exposed. And that's in spite of the DMCA making that a criminal offense.

Since no technical method can stop your customers from reading your code, you have to apply ordinary commercial methods.

  1. Licenses. Contracts. Terms and Conditions. This still works even when people can read the code. Note that some of your Python-based components may require that you pay fees before you sell software using those components. Also, some open-source licenses prohibit you from concealing the source or origins of that component.

  2. Offer significant value. If your stuff is so good -- at a price that is hard to refuse -- there's no incentive to waste time and money reverse engineering anything. Reverse engineering is expensive. Make your product slightly less expensive.

  3. Offer upgrades and enhancements that make any reverse engineering a bad idea. When the next release breaks their reverse engineering, there's no point. This can be carried to absurd extremes, but you should offer new features that make the next release more valuable than reverse engineering.

  4. Offer customization at rates so attractive that they'd rather pay you to build and support the enhancements.

  5. Use a license key which expires. This is cruel, and will give you a bad reputation, but it certainly makes your software stop working.

  6. Offer it as a web service. SaaS involves no downloads to customers.

Answer from S.Lott on Stack Overflow
Top answer
1 of 16
538

"Is there a good way to handle this problem?" No. Nothing can be protected against reverse engineering. Even the firmware on DVD machines has been reverse engineered and the AACS Encryption key exposed. And that's in spite of the DMCA making that a criminal offense.

Since no technical method can stop your customers from reading your code, you have to apply ordinary commercial methods.

  1. Licenses. Contracts. Terms and Conditions. This still works even when people can read the code. Note that some of your Python-based components may require that you pay fees before you sell software using those components. Also, some open-source licenses prohibit you from concealing the source or origins of that component.

  2. Offer significant value. If your stuff is so good -- at a price that is hard to refuse -- there's no incentive to waste time and money reverse engineering anything. Reverse engineering is expensive. Make your product slightly less expensive.

  3. Offer upgrades and enhancements that make any reverse engineering a bad idea. When the next release breaks their reverse engineering, there's no point. This can be carried to absurd extremes, but you should offer new features that make the next release more valuable than reverse engineering.

  4. Offer customization at rates so attractive that they'd rather pay you to build and support the enhancements.

  5. Use a license key which expires. This is cruel, and will give you a bad reputation, but it certainly makes your software stop working.

  6. Offer it as a web service. SaaS involves no downloads to customers.

2 of 16
429

Python, being a byte-code-compiled interpreted language, is very difficult to lock down. Even if you use a exe-packager like py2exe, the layout of the executable is well-known, and the Python byte-codes are well understood.

Usually in cases like this, you have to make a tradeoff. How important is it really to protect the code? Are there real secrets in there (such as a key for symmetric encryption of bank transfers), or are you just being paranoid? Choose the language that lets you develop the best product quickest, and be realistic about how valuable your novel ideas are.

If you decide you really need to enforce the license check securely, write it as a small C extension so that the license check code can be extra-hard (but not impossible!) to reverse engineer, and leave the bulk of your code in Python.

Discussions

Got my Python software finished. How do I secure it?
Some licensing libraries exist, like Cryptolens . They may not offer the exact methodology you've written above, but I doubt you want to reinvent the wheel. More on reddit.com
🌐 r/learnpython
87
223
August 2, 2022
Source code protection for potential on premise commercial use
Hi, I have developed an application for commercial using Python code and libs. My client wants me to install/deploy the server components on his local server managed by his IT guy and not cloud! I am wary of doing that as i understand it is “reasonably easy” to reverse engineer the python code. More on discuss.python.org
🌐 discuss.python.org
8
0
October 4, 2023
🌐
Kiuwan
kiuwan.com › blog › how-to-protect-python-code
How to Protect Python Code with Kiuwan
January 7, 2026 - However, it should also go beyond simply renaming your code. Using an automated code obfuscation tool like Dotfuscator or DashO allows you to add multiple layers of protection directly to your Python code.
🌐
PyPI
pypi.org › project › sourcedefender
sourcedefender · PyPI
3 weeks ago - SOURCEdefender is the easiest way to obfuscate Python code using AES-256 encryption. AES is a symmetric algorithm which uses the same key for both encryption and decryption (the security of an AES system increases exponentially with key length).
      » pip install sourcedefender
    
Published   Mar 25, 2026
Version   16.0.58
🌐
GitHub
github.com › Falldog › pyconcrete
GitHub - Falldog/pyconcrete: Protect your python script, encrypt it as .pye and decrypt when import it · GitHub
Protect your python script, encrypt .pyc to .pye and decrypt when import it
Starred by 749 users
Forked by 150 users
Languages   C 60.6% | Python 32.3% | Meson 2.1% | CMake 1.9% | Dockerfile 1.4% | Shell 1.0% | Just 0.7%
🌐
Codeclose
juanlao7.github.io › codeclose › documentation
Documentation | Codeclose — Protect and license your Python source code
$ cd protected $ python -m example1 JCYBGU-K4ZPE3-WC2ODA-774CLC 10 Product key is valid until 2050-08-17 0 1 1 2 3 5 8 13 21 34
🌐
Thales Group
docs.sentinel.thalesgroup.com › ldk › LDKdocs › Env-Win › Windows › Protecting_Python_Cython.htm
Protecting Python Applications With Cython
This script protects and packages the sample application for Python3 under Windows using the DEMOMA Batch Code, Cython, Microsoft Build Tools for Visual Studio 2019 (https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2019), Sentinel LDK Envelope, and pyinstaller.
Find elsewhere
🌐
Wibu-Systems
wibu.com › wibu-systems › products › codemeter protection suite › axprotector python
AxProtector Python - Wibu-Systems
AxProtector Python is the simple-to-use tool to protect Python applications against reverse engineering.
🌐
Reddit
reddit.com › r/learnpython › got my python software finished. how do i secure it?
r/learnpython on Reddit: Got my Python software finished. How do I secure it?
August 2, 2022 -

So I made a huge python tool for a client, created a GUI for it and deployed it to a EXE file with obfuscation. It's working perfectly.

Issue? I want to add licences. So when a user starts the .exe file it asks for a licence key which is validated to a web server and if the reply from the server is good, it decrpyts the code and moves foward. Betwen the code it randomly checks the licence with the server again if it's not OK it doesn't move forward and encrpyts the code again.

I want each licence to have access to only one computer so if two clients use the same licence the program starts but during the random checks if one check was done within 2 minutes from another computer then the request is denied so this way only 1 computer at a time can run the script.

I searched and searched and have found nothing on how to deploy this... Any ideas?

🌐
Python Software Foundation
wiki.python.org › python › Asking(20)for(20)Help(2f)How(20)do(20)you(20)protect(20)Python(20)source(20)code(3f).html
Asking for Help/How do you protect Python source code?
February 14, 2026 - Transformations provided by most Python code obfuscators include: Rename your code's internally used identifiers (variable names, function names, ... Remove or alter comments and docstrings. Even if you compile your code to .pyc files before distributing it, these transformations should increase the labor required to reverse-engineer your code. There are some other ways to obfuscate code object in runtime, like Pyarmor: Encrypt code object by DES to protect constants and literal strings.
🌐
Quora
quora.com › How-can-I-protect-my-Python-code-but-still-make-it-available-to-run
How to protect my Python code but still make it available to run - Quora
Answer (1 of 5): This answer describes how to protect sources created in python. I had tried and tested this method out on raspberrypi 3 (running jessie os). This should work similarly on ubuntu or any other Linux distro. However things will be different for Windows. get python (we are using 3) ...
🌐
Wibu-Systems
wibu.com › wibu-systems › resources › learning & virtual events › webinars › protecting python applications the simpler way
Protecting Python Applications The Simpler Way - Wibu-Systems
Wibu-Systems believes that only solid encryption can truly protect sensitive code from these threats. That is why Python support has been added to the popular CodeMeter Protection Suite. The traditional approach would be to transform Python code into a native application with tools like Cython and to then protect that application with CodeMeter’s powerful encryption.
🌐
ThinhDA
thinhdanggroup.github.io › secure-python-cython
Securing Your Python Code with Cython: A Comprehensive Guide - ThinhDA
August 25, 2023 - One of the major advantages of using Cython is its ability to protect Python code from being read by users. This is achieved by compiling Python code into C code, which is then compiled into machine code.
Top answer
1 of 5
3

As Python is an interpreted language by design; and as it compiles code to a bytecode (- which doesn't help the fact you're trying to conceal it, as bytecodes are easier to reverse -) there's no real secure way to hide your source code whereby it is not recoverable, as is true for any programming language, really.

Initially, if you'd wanted to work with a language that can't be so easily reversed- you should've gone for a more native language which compiles directly to the underlying architecture's machine code which is significantly harder to reproduce in the original language let alone read due to neat compiler optimizations, the overhead given by CISC et cetera.

However, some libraries that do convert your source code into an executable format (by packing the Python interpreter and the bytecode alongside it) can be used such as:

  • cx_Freeze - for freezing any code >=Python 2.7 for any platform, allegedly.
  • PyInstaller - for freezing general purpose code, it does state additionally that it works with third-party libraries.
  • py2exe -for freezing code into Windows-only executable format.

Or you might consider a substitute for this, which is code obfuscation which still allows the user to read the source code however make it near-to-impossible to read.

However, an issue brought up with this is that, it'd be harder for code addition as bad code obfuscation techniques could make the code static. Also, on the latter case, the code could have overhead brought by redundant code meant to fool or trick the user into thinking the code is doing something which it is not.

Also in general it negates the standard practice of open-source which is what Python loves to do and support.

So to really conclude, if you don't want to read everything above; the first thing you did wrong was choose Python for this, a language that supports open source and is open source as well. Thus to mitigate the issue you should either reconsider the language, or follow the references above to links to modules which might help aide basic source code concealment.

2 of 5
3

There are several tools which compile Python code into either (a) compiled modules usable with CPython, or (b) a self-contained executable.

https://cython.org/ is the best known, and probably? oldest, and it only takes a very small amount of effort to prepare a traditional Python package so that it can be compiled with Cython.

http://numba.pydata.org/ and https://pythran.readthedocs.io/ can also be used in this way, to produce Python compiled modules such that the source doesnt need to be distributed, and it will be very difficult to decompile the distributable back into usable source code.

https://mypyc.readthedocs.io is newer player, an offshoot of the mypy toolkit.

Nuitka is the most advanced at creating a self-contained executable. https://github.com/Nuitka/Nuitka/issues/392#issuecomment-833396517 shows that it is very hard to de-compile code once it has passed through Nuitka.

https://github.com/indygreg/PyOxidizer is another tool worth considering, as it creates a self-contained executable of all the needed packages. By default, only basic IP protection is provided, in that the packages inside it are not trivial to inspect. However for someone with a bit of knowledge of the tool, it is trivial to see the packages enclosed within the binary. However it is possible to add custom module loaders, so that the "modules" in the binary can be stored in unintelligible formats.

Finally, there are many Python to C/go/rust/etc transpilers, however these will very likely not be usable except for small subsets of the language (e.g. will 3/0 throw the appropriate exception in the target language?), and likely will only support a very limited subset of the standard library, and are unlikely to support any imports of packages beyond the standard library. One example is https://github.com/py2many/py2many , but a search for "Python transpiler" will give you many to consider.

🌐
Shawinnes
shawinnes.com › protecting-python
'Protecting' python code with Cython and Docker | Shaw Innes
March 27, 2024 - I was looking for a method to ... of IP protection. Knowing that this is a losing battle, and that I suspected that it was more about limiting curiosity rather than deliberate reverse engineering, I decided to investigate the use of cython to compile python via C into ...
🌐
Quora
quora.com › Is-it-possible-to-protect-Python-code-from-being-copied-or-pirated-if-at-least-one-module-needs-to-be-editable-by-a-user
Is it possible to protect Python code from being copied or pirated if at least one module needs to be editable by a user? - Quora
You likely can’t protect it. Even if you compile to byte-code, I have a “decompiler”. You don’t get all the variable and function names back, but it essentially turns the byte code back into Python source.
🌐
Medium
medium.com › @xpl › protecting-python-sources-using-cython-dcd940bb188e
Protecting Python Sources With Cython | by Vit Gordon | Medium
July 26, 2020 - Protecting Python Sources With Cython Distributing Python Programs As Compiled Binaries: How-To Protecting your Python sources from unwanted readers is easier said than done, because .pyc bytecode is …