Obfuscating your entire python project code with PyArmor the right way

tech kamar
2 min readOct 24, 2024

Pyarmor is a libray used to obfuscate your code or in simple english to make your code UNREADABLE .

It is easy to obfuscate a single file. But, when it comes to a project containing multiple python files. it becomes difficult due to import errors

So I have a solution for you. Follow the steps below

1. Make a copy of your Python Project

The code given below will overwrite all your files.

SO PLEASE MAKE A COPY OF ALL PYTHON PROJECT FOLDER

2. Create a newfile called “armor.py” in the root of your newly copied python project directory with contents given below

from os import walk,getcwd
import os

banned_directories = [".git"]
banned_files = []

def pyarmorit(dir_path,file_names):
for curr_file in file_names:
if curr_file in banned_files:
#skip it
continue

# Include only files ending with .py extension. Ignore others
if curr_file[-3:] == ".py":
input_file_name = f"{dir_path}/{curr_file}"
runarmor(input_file_name,dir_path)

def runarmor(input_file_name,output_dir):
command = f"pyarmor gen {input_file_name} --output={output_dir}"
os.system(command)

def path_not_banned_directory(dir_path):
for ban_directory in banned_directories:
if dir_path.find(ban_directory)>=0:
return False
return True

def main():
# Get Path of Current Directory
dir_path = getcwd()
res = []
for (dir_path, dir_names, file_names) in walk(dir_path):
# Avoid directories of .git and __pycache__
if path_not_banned_directory(dir_path):
pyarmorit(dir_path,file_names)

if __name__ == "__main__":
main()

In the above code,

banned_directories are the directories you can instruct the script to skip obfuscating python files

banned_files are specific python file names you want to instruct the script to avoid obfuscating

NOTE : Not all python files work with obfuscation. If you have a python file which goes as input to another python file (Not import).

Example: In flask, you can load configuration from a python file like this 👇

app.config.from_pyfile(config_file_path)

Such files should be excluded from OBFUSCATION process. Otherwise your application might break.

4.Run armor.py to obfuscate all files

python3 armor.py

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response