Looking for tips and gotchas for working with Python 3.5 zipapp feature

Paul Moore p.f.moore at gmail.com
Fri Sep 23 06:12:42 EDT 2016


On Tuesday, 20 September 2016 05:45:53 UTC+1, Malcolm Greene  wrote:
>  I really appreciate the detailed response. You answered all my
>  questions. I'm looking forward to testing out your pylaunch wrapper. 

Just one further note, which may or may not be obvious.

If your application uses external dependencies from PyPI, you can bundle them with your application using pip's --target option.

So suppose you have an application "myapp", in a directory called "myapp", which depends on (say) click and requests. So you have something like

    myapp
        __main__.py
        mymod
            __init__.py
            code.py
            morecode.py

To install requests and click ready for deployment, you can do

    pip install --target myapp requests click

That will give you the structure

    myapp
        __main__.py
        mymod
            __init__.py
            code.py
            morecode.py
        requests
        requests-2.11.1.dist-info
        click
        click-6.6.dist-info

You can delete the .dist-info directories if you wish, they are package metadata and not really important for deployment.

Then just bundle your application using "python -m zipapp myapp" and you have a fully standalone .pyz file, that doesn't require the user to have click or requests installed.

There are some external dependencies that won't work when bundled in a zipapp (most notably anything with a C extension) but for the majority of cases this works just fine.

Also, again in case you're not aware because it wasn't very well publicised when it was released, you can test your application before zipping it just by giving Python the name of the directory structured as above (python myapp). This won't catch dependencies that don't like being zipped, but it will save you having to go through zip/test/unzip/fix/rezip cycles during development.

Anyway, I hope this is useful.

Paul



More information about the Python-list mailing list