Search code examples
pythonzipapp

How do I compile a Python zipapp as optimized?


Assume the following code under src/app.py:

def main():
    assert False

if __name__ == "__main__":
    main()

Running this using python -o src/app.py will work fine as the assertions are disabled.

How can I package a zipapp (python -m zipapp src -m "app:main") such that when it is double-clicked or run, it'll automatically run as optimized?

I've tried changing the extension to .pyo and it still resulted in an AssertionError.


Solution

  • If I were you I would try the --python=<interpreter> option in order to write a shebang that contains the options you want (maybe python -O). It is somewhat unclear if writing options in the shebang is really supported or not (kind of related discussion here).

    You could also check if pex or shiv have options for what you want.