Member-only story
How to Obfuscate Python Scripts With PyArmor
Make your source code unreadable
Creating programs in Python is great: they’re fast to write and super easy to read. Sometimes, however, you don’t want your software code to be so easily understandable for many reasons ranging from trade secrets to enforcing paywalls.
The problem with distributing classic Python programs is that anyone can open the files in an editor and read the source code, possibly modifying parts that you, as a developer, don’t want users to touch, remove in-app paywalls, or get insights into how your program works.
As a developer, you can use code obfuscation to make the program files ideally unreadable to a human but still executable by a computer, thus preventing undesired eyes from sneaking into your application code. Obfuscation is similar to encryption in that the goal is the same: concealing a message, but encryption requires decryption to use the information, while obfuscated code can generally be run directly.
Installing PyArmor
To use PyArmor, you can install it through pip
:
pip install pyarmor
If you wish to uninstall PyArmor, follow the instructions on their official documentation in the “Clean uninstallation” section.
Basic obfuscation with PyArmor
Let’s first create a simple Python script named script.py
:
Obfuscating a Python script with PyArmor is as easy as running the following command in the console:
pyarmor obfuscate script.py
This will generate a dist
folder containing the obfuscated Python file with the same name as the original along with runtime dependencies as shown in the folder structure below:
