pythongunicornzipapppyz

Python pyz executable is not using local dependencies


I have an application which has a lot of dependencies.

I have tried to package it as a .pyz executable with all of its dependencies using this tutorial ZipAppTutorialByRealPython

In one of the systems that I am deploying, has a zipp package version as 0.6.0 installed My pyz is packaging zipp 3.6.0 but this shouldn't create a problem as I am packaging all my dependencies in the executable.

But when I run the gunicorn app, I'm seeing a version conflict error

    raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.ContextualVersionConflict: (zipp 0.6.0 (/usr/lib/python3.6/site-packages), Requirement.parse('zipp>=3.1.0; python_version < "3.10"'), {'importlib-resources'})

I'm unable to figure out the reason for this.

Some contextual info - This application is run using gunicorn

Command to create the executable

python3 -m zipapp src -p "/usr/bin/env python3" -m 'gunicorn.app.wsgiapp:run' -o service.pyz

Tree Structure of Project

src|
   |
   |service|
           | __init__.py
           | __main__.py
           | serviceImplPackages

   |all the pip packages

Solution

  • Seems like some there was some issue in the tutorial or my understanding of pyz executables is not up to date

    When I ran the executable as is, it was conflicting with package versions in global site-packages, and was not referring to the local ones.

    Solution: (worked for me)

    This works even if you're running your executable as part of a bash script

    python3 -m venv venv-dir
    source venv-dir/bin/activate
    python3 package.pyz
    
    # It now uses the packages bundled with the pyz.
    
    # you can deactivate after this if you like but I don't believe it would make any difference. I tested both ways. Please let me know if it does