1

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.

Bharel
  • 23,672
  • 5
  • 40
  • 80

1 Answers1

1

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.

Bharel
  • 23,672
  • 5
  • 40
  • 80
sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • 1
    Per your suggestion, I looked at the shebang and [this](https://docs.python.org/3/using/windows.html#arguments-in-shebang-lines) seems to be exactly what I need. I'm not sure why does the discussion say otherwise. Yet to test if it actually work (I fear that escaping the quotes would be an issue, i.e. how to differentiate between `"python -o"` as interpreter name vs name+arguments), but I would modify your answer to accompany that link :-) – Bharel Feb 22 '23 at 21:12
  • 1
    Seen from the point of view of your use case, the issue seems to be that there is no specification for *shebangs* (say for example in POSIX). The implementations on most common systems seems to be largely that something like `#!python -O` (maybe even `!#/usr/bin/en python -O`) will behave as one can expect, but it is not strictly guaranteed, there is a small leap of faith to be made. -- The documentation you linked is for the `py` launcher on Windows, which appears to have been designed to mimick the Linux/UNIX behavior. -- All in all it seems to be a reasonably viable solution. – sinoroc Feb 22 '23 at 22:18
  • thank you for your detailed response, I highly appreciate it. I will mark the question as answered as soon as I test and see that it works :-) – Bharel Feb 22 '23 at 23:17