Encrypt Python source files and decrypt them on the fly during runtime.
Using pip
:
pip install sourceprotected
sourceprotected path/to/src(.py)
Using key: fHC79...
Using encryption time: 1694...
With sourceprotected
installed in your current environment, you can run/import encrypted files/modules as long as the SOURCEPROTECTED_KEY
environment variable is set:
encrypted.py
:
# -*- coding: sourceprotected -*-
-----BEGIN SOURCEPROTECTED FILE-----
gAAAAABk-eJaJarzMWy70GTRkRi6kjaD-zJy
-fHX_0G5VHTZjEKxJUvFhVWfWpX_j3LydmJo
pn7YrAj5CSqqPWgxUY_dSk5ELA==
-----END SOURCEPROTECTED FILE-----
main.py
:
import encrypted # Will decrypt encrypted.py on the fly.
Finally:
SOURCEPROTECTED_KEY=Bk92p... python -B main.py
sourceprotected
uses a custom codec that will decrypt the file content using the SOURCEPROTECTED_KEY
environment variable.
This codec is registered on startup by using a .pth
file.
Credits goes to https://github.com/asottile-archive/future-fstrings for the implementation idea.