Skip to main content

Minify/obfuscate Python 3 source code

Project description

mnfy — minify/obfuscate Python 3 source code

Web Pages

What the heck is mnfy for?

The mnfy project was created for two reasons:

  • To show that shipping bytecode files without source, as a form of obfuscation, is not the best option available

  • Provide a minification of Python source code when total byte size of source code is paramount

When people ship Python code as only bytecode files (i.e. only .pyo files and no .py files), there are couple drawbacks. First and foremost, it prevents users from using your code with all available Python interpreters such as Jython and IronPython. Another drawback is that it is a poor form of obfuscation as projects like Meta allow you to take bytecode and reverse-engineer the original source code as enough details are kept that the only details missing are single-line comments.

When the total number of bytes used to ship Python code is paramount, then you want to minify the source code. Bytecode files actually contain so much detail that the space savings can be miniscule (e.g. the decimal module from Python’s standard libary, which is the largest single file in the stdlib, has a bytecode file that is only 5% smaller than its original source code).

Usage

A note about version numbers and Python version compatibility

The version number for mnfy is PEP 386 compliant, taking the form of PPP.FFF.BBB. The FFF.BBB represents the feature and bugfix version numbers of mnfy itself. The PPP portion of the version number represents the Python version that mnfy is compatible with: '{}{}'.format(*sys.version_info[:2]).

The Python version that mnfy is compatible with is directly embedded in the version number as Python’s AST is not guaranteed to be backwards-compatible. This means that you should use each version of mnfy with a specific version of Python. Since mnfy works with source code and not bytecode you can safely use mnfy on code that must be backwards-compatible with older versions of Python, just make sure the interpreter you use with mnfy is correct and can parse the source code (e.g. just because the latest version of mnfy only works with Python 3.3 does not mean you cannot use that release against source code that must work with Python 3.2, just make sure to use a Python 3.3 interpreter with mnfy and that the source code can be read by a Python 3.3 interpreter).

Command-line Usage

TL;DR: pass the file you want to minify as an argument to mnfy and it will print to stdout the source code minified such that the AST is exactly the same as the original source code. To get transformations that will change the AST to varying degrees you will need to specificy various flags.

See the help message for the project for full instructions on usage:

python3 -m mnfy -h
python3 mnfy.py -h

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mnfy-33.0.1.tar.gz (12.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page