Using PyArmor to Protect Python Code: Features, Quick Start, Project Management, and Command Reference
This guide explains how PyArmor encrypts and protects Python scripts through seamless replacement, dynamic encryption, licensing, hardware binding, and packaging, providing step‑by‑step installation, usage commands, project management techniques, and a comprehensive command reference for developers.
PyArmor is a Python code protection tool that encrypts scripts, prevents code leakage, and can bind encrypted scripts to hardware devices or set expiration dates. It works by replacing the original script with an encrypted version that runs using the pytransform module.
Tool Features
Seamless Replacement : Encrypted scripts run like normal Python scripts with the additional pytransform extension.
Dynamic Encryption : Scripts are re‑encrypted after each function execution, enhancing runtime security.
Packaging : Combine with PyInstaller to bundle encrypted scripts into standalone executables.
Licensing Options : Set usage expiration, bind to disk serial numbers, MAC addresses, IP addresses, or add custom authentication.
Quick Usage
<code># Install PyArmor
pip install pyarmor
# Upgrade to latest version
pip install --upgrade pyarmor
# Install graphical WebUI
pip install pyarmor-webui
pyarmor-webui</code> <code># Uninstall completely
pip uninstall pyarmor
rm -rf ~/.pyarmor
rm -rf ~/.pyarmor_capsule.zip
rm /path/to/project/.pyarmor_config</code> <code># Encrypt a script
pyarmor obfuscate foo.py
# Encrypt a single module
pyarmor obfuscate --exact foo.py
# Encrypt an entire package recursively
pyarmor obfuscate --recursive --output dist/mypkg mypkg/__init__.py</code> <code># Run the encrypted script
cd dist
python foo.py</code> <code># Create a license file with hardware binding and expiration
pyarmor licenses \
--expired "2018-12-31" \
--bind-disk "100304PBN2081SF3NJ5T" \
--bind-mac "70:f1:a1:23:f0:94" \
--bind-ipv4 "202.10.2.52" \
r001
# Encrypt using the new license
pyarmor obfuscate --with-license licenses/r001/license.lic foo.py</code> <code># Pack encrypted script into an executable
pip install pyinstaller
pyarmor pack foo.py</code>Project Management
Projects allow managing many scripts with a configuration file, supporting incremental encryption, selective inclusion, custom protection modes, and easier maintenance.
<code># Initialize a project
cd examples/pybench
pyarmor init --entry=pybench.py
# Create a project configuration
pyarmor init --src=examples/pybench --entry=pybench.py projects/pybench
# Build the project (encrypt all scripts)
cd projects/pybench
pyarmor build
# Modify manifest to include/exclude files
pyarmor config --manifest "include *.py, prune dist, prune test"
pyarmor build
# Run the encrypted script
cd dist
python pybench.py</code>Command Reference
pyarmor obfuscate – encrypt scripts
pyarmor licenses – generate license files
pyarmor pack – encrypt and package into a standalone executable
pyarmor hdinfo – retrieve hardware information
pyarmor init , config , build , info , check – project‑related commands
pyarmor benchmark , register , download , runtime – less frequently used commands
For more details, refer to the original article linked at the end of the guide.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.