I was inspired by this article as I think about obfuscation and reducing the storage requirements of my python app for an embedded device. I compileall from the command line and then remove all *.py files.
My app has many modules (subdirectories with __init__.py
files), and I'm having issues simply stripping away all of the .py
source files. If I remove all the py source files, I get import errors. I experimented with only stripping out .py source files that are not the __init__.py
files and still get import errors. The pycache directory also contains both *.pyc (interpreter-generated) and *.pyo (compileall-generated) files. Do I only need one type?
I've tested compileall with and without the legacy -b
option but still get the same import errors for a file that understandably no longer exists.
from mypackage.mypyfile import my_main
Is there more of a standard procedure for creating a pyo-only distribution?
# Clean potentially pre-existing cache
find mydir -type f -name "*.py[co]" -delete
find mydir -type d -name "__pycache__" -exec rm -r "{}" +
sync
# Byte-compile, ignoring .git subdirectories
python3 -m compileall -b -f -x r'[/\\][.]git' mydir
# Remove all source files
find mydir -type f -name "*.py" -delete
related: Is there a way to combine a python project codebase that spans across different files into one file?