简体   繁体   中英

Deploy python app with setup.py and Pipfile

I have a flask app with a Pipfile and run pipenv run python setup.py sdist to create a package. I copy the package to another system.

Usually I would install it with pip and all requirements declared in install_requires in setup.py would be installed automatically.

How can I install the package and its requirements and make use of the Pipfile.lock?

If I install the package with pip, I could run pipenv install --deploy in the directory it was installed, but how can I reliably retrieve the directory my package was installed in? Is this a good way to do this?

I'm looking for the best way to install a python app with setuptools and pipenv.

I'll share my thoughts on this.

First, the abstract dependencies of your project are to be listed in setup.py's install_requires. They should not be pinned if possible and reading dependencies in setup.py from somewhere else is not recommended.

Thus, if you install a package created with python setup.py sdist , that project's Pipfile.lock will not be used. Instead the user of the package is reponsible for locking the dependencies and installing the package into a virtualenv.

To use our Pipfile.lock we need a different approach to deployment. I've collected a few.

1) git clone the repository on target machine or rsync -r it to target machine. Run pipenv install --deploy in cloned project directory. There are several ways of using the virtualenv:

  • Launch the app with pipenv run <appname> from the cloned project directory. Make sure you are the same user who created the virtualenv.
  • Retrieve the virtualenv location by running pipenv --venv from the cloned project directory as the same user who created the virtualenv and use it directly for running your app.
  • Set PIPENV_VENV_IN_PROJECT=1 environment variable before running pipenv install --deploy to get a consistent virtualenv location that you can then use directly for running your app.
  • Before running pipenv, manually create a virtualenv then run pipenv from there. Pipenv will automatically use that virtualenv instead of creating a new one. See https://stackoverflow.com/a/49388414/7662112 for a complete workflow.

2) Use pipenv install --system --deploy to set up the virtualenv from your Pipfile.lock in docker. Then just use the docker image for deployment.

3) Dump Pipfile.lock into a requirements.txt with pipenv lock --requirements > requirements.txt and build a deb package using dh-virtualenv .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM