Skip to content
PyArmorDEMO

Protect your own project

⏱ 10 minutes · The workshop's workflow, applied to a project you already have.

Commands below use mypkg/ (your package) and main.py (its entry script). Rename to match yours.

1. Build where you run

Build with the same Python minor version, OS and CPU architecture you deploy to. A build made on Python 3.12 does not load on 3.11 (chapter 06). Inside Docker, use the same base image for building and running.

Terminal
python --version
pip install pyarmor==9.2.7

2. Obfuscate

Terminal
pyarmor gen -O dist app.py                    # one script
pyarmor gen -O dist -r main.py mypkg          # entry script + package, recursive
pyarmor gen -O dist -r --exclude "mypkg/tests" main.py mypkg   # leave a folder out

Expected: INFO obfuscate scripts OK, and dist/ with your protected files plus pyarmor_runtime_000000/.

3. Copy what PyArmor does not

Only .py files are written to dist/. Data files (JSON, templates, static files) are not copied, and the app fails without them:

Output
FileNotFoundError: [Errno 2] No such file or directory: '.../dist/mypkg/config.json'
Terminal
cp mypkg/config.json dist/mypkg/
cp -r mypkg/templates dist/mypkg/

4. Check it

Terminal
cd dist
python main.py

Then run your test suite against dist/ instead of the source. tests/conftest.py shows a small switch for that, and scripts/verify.py checks that every module is obfuscated and no names leak.

5. Ship

Ship dist/ (protected code + runtime) and your pinned dependencies, never the source. For a container, copy the two-stage pattern in app/backend/Dockerfile.

If you useNote
uvicorn / gunicorn "mypkg.main:app"Works when dist/ is the working directory or on PYTHONPATH
PyInstallerUse pyarmor gen --pack onedir; import your app directly in the entry script (chapter 08)
Django / FlaskCopy templates, static files and migrations data as in step 3

Before production

  • No secrets in code, protected or not (chapter 11).
  • Obfuscation raises the effort to read your code. It does not protect the machine it runs on.
  • Pin PyArmor, register a license for commercial use, and keep the build in CI (chapter 12).