"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.

🌐
Reddit
reddit.com › r/learnpython › encrypt python .py file
r/learnpython on Reddit: Encrypt Python .py file
October 26, 2022 -

Knowledge grows with sharing and I am truly believer. With same beliefs I shared my python script with my colleague because he wanted it. He shared with more people who made that program with commercial glaze and claimed some benefits which I found recently. Now they are asking more scripts because I have many programs to automate lots of tasks. How can I encrypt my scripts so people can use but not copy source code for own benefits.

Discussions

Make your python script safety?
Make your python script SAFETY Background We know when we run Python script we can get the .pyc files. But there are some tools that can decompile .pyc files to .py files. It is not a good behavior but in China, a lot of people are using it. How to make your python script safety ? More on discuss.python.org
🌐 discuss.python.org
11
0
August 29, 2024
How to protect source code when distributing docker images?
Good lawyer. Good copyright notice. Good contract. Python is inherently open. You could write it in another language, but they could still reverse engineer that. That is a legal question, not a docker/python/software question. Edit: the other option is a managed API you run yourselves off-premises, but if they want it on-site, do it and let legal handle the logistics. More on reddit.com
🌐 r/docker
40
41
December 11, 2021
How to protect source code in a docker container?
No, that is not possible. As long as the container is running, all files are accessible. More on reddit.com
🌐 r/docker
13
1
February 27, 2019
How to pack / obfuscate / encrypt a Django project?
In my experience this isn't do-able and the way that you protect your intellectual property is via legal agreements with the customer. More on reddit.com
🌐 r/django
14
5
May 16, 2018
🌐
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
ext: Able to assign customized encrypted file extension. Which default is .pye. ... $ pyecli compile --pye -s=<your py script> $ pyecli compile --pye -s=<your py module dir> $ pyecli compile --pye -s=<your py module dir> -e=<your file extension> ... main.py encrypted as main.pye, it can't be executed by normal python.
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%
🌐
The Python Code
thepythoncode.com › article › encrypt-decrypt-files-symmetric-python
How to Encrypt and Decrypt Files in Python - The Python Code
View Full Code Auto-Generate My Code · Sharing is caring! Using different hashing algorithms such as SHA-2, SHA-3 and BLAKE2 in Python using hashlib built-in module for data integrity. Learn how to add and remove passwords to PDF files using PyPDF4 library, as well as using pyAesCrypt to encrypt and decrypt PDF files in Python
🌐
99RDP
99rdp.com › home › encrypting python (.py) files: a comprehensive guide
Encrypting Python (.py) Files: A Comprehensive Guide
August 10, 2024 - PyArmor generates an encrypted .py file, which can be distributed with a license file to control access. Cython: Cython compiles Python code into C, then into a shared library (.so or .dll), which is much harder to reverse-engineer.
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to hide/encrypt/obfuscate any python program
How to Hide/Encrypt/Obfuscate any Python Program | Towards Data Science
March 5, 2025 - For the purpose of obfuscation, we will be using a python package called pyarmor. We might sometimes face a situation where we need to provide code directly to a client for obvious reasons, but by doing so, we will lose control of the code. In such cases, we might we encrypt the codes to protect it, retain control and add some fallback condition to control our dependency, just like if we provided code for using only for a certain amount of time.
Find elsewhere
🌐
Codeclose
juanlao7.github.io › codeclose › documentation
Documentation | Codeclose — Protect and license your Python source code
Codeclose obfuscates all your code by replacing all identifiers (i.e., variables, functions, classes, etc.) with random names and converting all strings into numeric operations that are computed in runtime. In order to respect API’s and keep reflection working, it is possible to specify a list of identifiers and attributes that must be kept intact. After the obfuscation, Codeclose encrypts the result with AES-256.
🌐
Medium
medium.com › @osama.ghandour › how-to-encrypt-secure-and-close-python-file-for-not-getting-its-code-e5c7b3b3cccd
How to encrypt , secure and close python file for not getting its code ? | by Osama Ghandour Geris | Medium
October 27, 2023 - Examples include `pyobfuscate` and `pyarmor`. Note that obfuscation doesn’t provide strong security but can make the code harder to understand. 3. **Compiled Executables**: Convert your Python code into a standalone executable using tools like `PyInstaller` or `cx_Freeze`. This method bundles the Python interpreter with your code, making it more challenging to access the source.
🌐
Quora
quora.com › How-do-you-encrypt-a-script-in-Python
How to encrypt a script in Python - Quora
You cannot encrypt Python code. Or, code in any other programming language, for that matter. Since the program itself can be reverse-engineered, there is no way to do it.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-encrypt-and-decrypt-strings-in-python
How to Encrypt and Decrypt Strings in Python? - GeeksforGeeks
August 14, 2024 - Install the python cryptography library with the following command. ... Then generate an encryption key, that can be used for encryption and decryption. Convert the string to a byte string, so that it can be encrypted.
🌐
GitHub
github.com › pycrypt123 › pycryptor
GitHub - pycrypt123/pycryptor: Encrypt python source code
pycryptor is a commercial solution for the encryption of python code file. pycryptor encrypt python source code using RC4, which is a fast and highly secure data encryption algorithm.
Starred by 24 users
Forked by 11 users
Languages   C 71.9% | Python 22.0% | Makefile 6.1% | C 71.9% | Python 22.0% | Makefile 6.1%
🌐
Quora
quora.com › What-s-the-best-method-to-encrypt-Python-code
What’s the best method to encrypt Python code? - Quora
Answer (1 of 5): Do you mean encrypting your compiled pyc file, not really possible if you mess up with it your code won't execute. Since python is first compiled to byte code, its really not hard to reverse engineer it. One thing you could do though is to use tools like py2exe to convert your ...
🌐
Codementor
codementor.io › python › tutorial › python-encryption-message-in-python-via-reverse-cipher
Simple Python Encryption: How to Encrypt a Message | Codementor
Instead of appending the print() ... to return our code. ... Please bear in mind this is possible in Python 2 and not previous versions. Now let’s fix our attention on basic programming concepts such as the lens() , data type, comparison operators and others which we will need for our encrypting ...
🌐
Allplan
pythonparts.allplan.com › 2023 › manual › features › encrypt_source_code
Encrypt source code - Python API Documentation
The encrypted files are given the extension .pye and are used if no .py file is available. Instead of the .py files the .pye files can be delivered to the customer. The encryption of the source code can be executed by the PythonPart EncryptPythonPartScript, located in \etc\Examples\PythonP...
🌐
TutorialsPoint
tutorialspoint.com › cryptography_with_python › cryptography_with_python_quick_guide.htm
Cryptography with Python - Quick Guide
You can observe the following output when you implement the code given above − · (myenv) D:\Projects\python\myenv>py main.py Enter your message: "HELLO" E for Encrypt, D for Decrypt: E Enter 26 ALPHA key (leave blank for random key): IQRYVOCPNALMHTZGKFXJSEBDWU Using key: IQRYVOCPNALMHTZGKFXJSEBDWU Result: "PVMMZ" In this chapter, we will focus on testing substitution cipher using various methods, which helps to generate random strings as given below −
🌐
PyPI
pypi.org › project › pyconcrete
pyconcrete · PyPI
pyconcrete will decrypt the source file and then launch python interpreter to do the normal python behavior. ... when your script do import MODULE, pyconcrete import hook will try to find MODULE.pye first and then decrypt MODULE.pye via _pyconcrete.pyd and execute decrypted data (as .pyc content) encrypt & decrypt secret key record in _pyconcrete.pyd (like DLL or SO) the secret key would be hide in binary code, can't see it directly in HEX view
      » pip install pyconcrete
    
Published   Sep 01, 2025
Version   1.2.0
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › using the raspberry pi › beginners
Encrypt Python Code - Raspberry Pi Forums
(or observe the memory to see the plain information somewhen in the memory dump) { I only give negative feedback } RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB) ... The best way is to encrypt the login, keep that as a string and then hash the password the user enters and if it ...
🌐
AskPython
askpython.com › home › how to write an encryption program in python?
How to Write an Encryption Program in Python? - AskPython
March 16, 2023 - In this approach, we will take the input from the user, get the ASCII code of each character in the message and print the cipher text with a key added to the ASCII value of each character.