What's the standard way to package a python project with dependencies?

前端 未结 3 1557
野性不改
野性不改 2021-01-14 00:55

I have a python project that has a few dependencies (defined under install_requires in setup.py). My ops people requires a package to be self contained and only

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 01:18

    There are a few nuances to how pip works. Unfortunately, using --prefix vendor to store all the dependencies of the project doesn't work if any of those dependencies, or dependencies of dependencies are installed into a place where pip can find them. It will skip those dependencies and just install the rest to your vendor folder.

    In the past I've used virtualenv's --no-site-packages option to solve this issue. At one company we would ship the whole virtualenv, which includes the python binary. In the interest of only shipping the dependencies, you can combine using a virtualenv with the --prefix switch on pip to give yourself a clean environment that installs to the right place.

    I'll provide an example script that creates a temporary virtualenv, activates it, then installs the dependencies to a local vendor folder. This is handy if you are running in CI.

    #!/bin/bash
    
    tempdir=$(mktemp -d -t project.XXX) # create a temporary directory
    trap "rm -rf $tempdir" EXIT         # ensure it is cleaned up
    # create the virtualenv and exclude packages outside of it
    virtualenv --python=$(which python2.7) --no-site-packages $tempdir/venv
    # activate the virtualenv
    source $tempdir/venv/bin/activate    
    # install the dependencies as above
    pip install -r requirements.txt --prefix=vendor
    

提交回复
热议问题