"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
🌐
PyPI
pypi.org › project › sourcedefender
sourcedefender · PyPI
2 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
🌐
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
Discussions

Encrypt Python .py file
Simply put, you don't. Obfuscating the code is possible, but doesn't really prevent anyone from modifying or seeing it, and it can be made readable again. You could create executable binaries, but often these would just be executable ZIP-files that package the Python interpreter with your code. You could transpile the code to C and compile that (Cython and Nuitka are examples), which could work, but even that won't stop the most curious individuals from decompiling it. The thing with encryption is that in order to even run an encrypted program, you need to decrypt it, which needs the decryption key. So if the users could run it, they already have the key. The option most resistant to such tampering would be to host the code on a web service and have the users use that, as they don't have access to the back-end where your Python code would run. But that doesn't work for everything. My advice? Have a license on your code that meets your needs. Then you would at least have some leverage if you need to challenge someone misusing your work. More on reddit.com
🌐 r/learnpython
52
130
October 26, 2022
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
Encrypt/Decrypt With Custom Dictionary
Dear community, I’ve created a code to Encrypt/Decrypt lists of words using a custom dictionary. Although the Encrypt function work as expected, the Decrypt option is causing me some problems, I’ll explain why. The ent… More on discuss.python.org
🌐 discuss.python.org
9
0
February 5, 2023
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.

🌐
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%
🌐
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() ... 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 ...
🌐
GitHub
github.com › pycrypt123 › pycryptor
GitHub - pycrypt123/pycryptor: Encrypt python source code
Using pycryptor, your source code files are encrypted with your own key (64 bytes). Without the leak of your own key, it's almost impossible for anyone else to decrypt your code files. pycryptor is easy to use.
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%
Find elsewhere
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-encrypt-and-decrypt-strings-in-python
How to Encrypt and Decrypt Strings in Python? - GeeksforGeeks
August 14, 2024 - The public key is used to encrypt the data and the private key is used to decrypt the data. By the name, the public key can be public (can be sent to anyone who needs to send data).
🌐
Allplan
pythonparts.allplan.com › 2023 › manual › features › encrypt_source_code
Encrypt source code - Python API Documentation
The encryption of the source code can be executed by the PythonPart EncryptPythonPartScript, located in \etc\Examples\PythonParts\ToolsAndStartExamples.
🌐
Des
des.co.uk › encryption › encrypt-python-code
Encryption for Python code | DES
Our encryption wrapper for python protects Python code against reverse engineering.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › using the raspberry pi › beginners
Encrypt Python Code - Raspberry Pi Forums
encryption of entire python script code (*.py), is as long as i know, not possible. even if you have your python code available as compiled only (*.pyc), you are able to see the strings and names inside very easily. you never should put plain credential information in your source code...
🌐
Medium
medium.com › @_Ankit_Malviya › securing-your-python-code-a-complete-guide-to-encrypting-multiple-files-and-packages-with-pyarmor-1a9c158c6f7e
Securing Your Python Code: A Complete Guide to Encrypting Multiple Files and Packages with PyArmor | by Dr. Ankit Malviya | Medium
November 24, 2025 - Think of it as wrapping your code in a secure vault that only the Python interpreter can unlock — and only under the conditions you specify. ... For commercial projects, you’ll need a license. PyArmor offers different tiers based on your needs, but the basic version is free for evaluation and non-commercial use. Let’s start simple. Here’s how to encrypt a single Python script:
🌐
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.
🌐
SKOOL OF CODE
skoolofcode.us › home › cryptography with python
Cryptography Code To Encrypt String In Python
July 31, 2022 - Read on to get cryptography code to encrypt a string in python with a coding tutor for kids to hide the actual text. It was an efficient way to encrypt a string.
🌐
TutorialsPoint
tutorialspoint.com › cryptography_with_python › cryptography_with_python_quick_guide.htm
Cryptography with Python - Quick Guide
Base64 is also called as Privacy enhanced Electronic mail (PEM) and is primarily used in email encryption process. Python includes a module called BASE64 which includes two primary functions as given below − · base64.decode(input, output) − It decodes the input value parameter specified and stores the decoded output as an object. Base64.encode(input, output) − It encodes the input value parameter specified and stores the decoded output as an object. You can use the following piece of code to perform base64 encoding −
🌐
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.