2

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?

Community
  • 1
  • 1
tarabyte
  • 17,837
  • 15
  • 76
  • 117
  • What device are you targeting? Have you determined the total storage requirements of the app? How does that compare to the size of the source files? It's a rare app where source files are a non-negligible fraction of the total size. – Antimony Nov 04 '15 at 03:24

1 Answers1

1

It appears that the pyo-only distribution must be invoked with the same interpreter and interpreter flags as those used to byte-compile:

python3 -OO myapp.py

http://bugs.python.org/issue570640

tarabyte
  • 17,837
  • 15
  • 76
  • 117