How to Protect Python Code with PyArmor
Ohidur Rahman Bappy
MAR 22, 2025
How to Protect Python Code with PyArmor
PyArmor is a command-line tool designed to obfuscate Python scripts, bind them to specific machines, or set expiration dates. It enhances the security of your Python code in several ways:
Features of PyArmor
- Obfuscation of Code Object: Protects constants and literal strings.
- Runtime Code Obfuscation: Applies to the
co_code
of each function. - Automatic Frame Local Clearing: Clears
f_locals
as soon as a code object finishes execution. - License Verification: Ensures that obfuscated scripts run only with a valid license.
Installation
To install PyArmor, simply run:
pip install pyarmor
Understanding the Output
When you obfuscate a Python file like foo.py
using PyArmor, the output includes:
foo.py
pytransform/
__init__.py
_pytransform.so or _pytransform.dll or _pytransform.dylib
The dist/foo.py
becomes the obfuscated script, which includes:
from pytransform import pyarmor_runtime
pyarmor_runtime()
__pyarmor__(__name__, __file__, b'\x06\x0f...', 1)
Runtime Package
The pytransform
folder, known as the Runtime Package, is crucial. As long as this package is accessible within any Python path, the obfuscated script (dist/foo.py
) can function like a normal Python script.
Supported Platforms
- Python 2.5, 2.6, 2.7, and Python 3
- Windows (win32, win_amd64), Linux (linux_i386, linux_x86_64), macOS (macosx_x86_64)
- Embedded Platforms: Raspberry Pi, Banana Pi, Orange Pi, TS-4600 / TS-7600
Quick Start Guide
Obfuscate Scripts
To obfuscate a script:
pyarmor obfuscate foo.py
Execute Obfuscated Scripts
cd dist
python foo.py
Obfuscate with Expired License
For scripts with an expiration date:
pyarmor licenses --expired 2018-12-31 r001
pyarmor obfuscate --with-license licenses/r001/license.lic foo.py
Bundle Obfuscated Scripts
To pack obfuscated scripts into one bundle:
pip install pyinstaller
pyarmor pack foo.py
PyArmor Web UI
For a graphical interface, use the web-ui package:
pip install pyarmor-webui
Start the Web UI and open the page in your browser:
pyarmor-webui