Skip to content

Latest commit

 

History

History
193 lines (138 loc) · 7.87 KB

managing-dependencies.rst

File metadata and controls

193 lines (138 loc) · 7.87 KB

Managing Application Dependencies

The package installation tutorial <installing-packages> covered the basics of getting set up to install and update Python packages.

However, running these commands interactively can get tedious even for your own personal projects, and things get even more difficult when trying to set up development environments automatically for projects with multiple contributors.

This tutorial walks you through the use of Pipenv to manage dependencies for an application. It will show you how to install and use the necessary tools and make strong recommendations on best practices.

Keep in mind that Python is used for a great many different purposes, and precisely how you want to manage your dependencies may change based on how you decide to publish your software. The guidance presented here is most directly applicable to the development and deployment of network services (including web applications), but is also very well suited to managing development and testing environments for any kind of project.

Developers of Python libraries, or of applications that support distribution as Python libraries, should also consider the poetry project as an alternative dependency management solution.

Recommendation caveats (as of April 2020)

While this tutorial covers the pipenv project as a tool that focuses primarily on the needs of Python application development rather than Python library development, the project itself is currently working through several process and maintenance issues that are preventing bug fixes and new features from being published (with the entirety of 2019 passing without a new release).

This means that in the near term, pipenv still suffers from several quirks and performance problems without a clear timeline for resolution of those isses.

While this remains the case, project maintainers are likely to want to investigate other-dependency-management-tools for use instead of, or together with, pipenv.

Assuming the April 2020 pipenv release goes ahead as planned, and the release after that also remains on track, then this caveat on the tutorial will be removed. If those releases don't remain on track, then the tutorial itself will be removed, and replaced with a discussion page on the available dependency management options.

Installing Pipenv

Pipenv is a dependency manager for Python projects. If you're familiar with Node.js' npm or Ruby's bundler, it is similar in spirit to those tools. While pip alone is often sufficient for personal use, Pipenv is recommended for collaborative projects as it's a higher-level tool that simplifies dependency management for common use cases.

Use pip to install Pipenv:

pip install --user pipenv

Note

This does a user installation to prevent breaking any system-wide packages. If pipenv isn't available in your shell after installation, you'll need to add the user base's binary directory to your PATH. See Installing to the User Site for more information.

Installing packages for your project

Pipenv manages dependencies on a per-project basis. To install packages, change into your project's directory (or just an empty directory for this tutorial) and run:

cd myproject
pipenv install requests

Pipenv will install the Requests library and create a Pipfile for you in your project's directory. The Pipfile is used to track which dependencies your project needs in case you need to re-install them, such as when you share your project with others. You should get output similar to this (although the exact paths shown will vary):

Creating a Pipfile for this project...
Creating a virtualenv for this project...
Using base prefix '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6'
New python executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python3.6
Also creating executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: ~/.local/share/virtualenvs/tmp-agwWamBd
Installing requests...
Collecting requests
  Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests)
  Using cached idna-2.6-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Using cached chardet-3.0.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
  Using cached certifi-2017.7.27.1-py2.py3-none-any.whl
Installing collected packages: idna, urllib3, chardet, certifi, requests
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22

Adding requests to Pipfile's [packages]...

Using installed packages

Now that Requests is installed you can create a simple main.py file to use it:

import requests

response = requests.get('https://httpbin.org/ip')

print('Your IP is {0}'.format(response.json()['origin']))

Then you can run this script using pipenv run:

pipenv run python main.py

You should get output similar to this:

Your IP is 8.8.8.8

Using pipenv run ensures that your installed packages are available to your script. It's also possible to spawn a new shell that ensures all commands have access to your installed packages with pipenv shell.

Next steps

Congratulations, you now know how to effectively manage dependencies and development environments on a collaborative Python project! ✨ 🍰 ✨

If you're interested in creating and distributing your own Python packages, see the tutorial on packaging and distributing packages <distributing-packages>.

Note that when your application includes definitions of Python source packages, they (and their dependencies) can be added to your pipenv environment with pipenv install -e <relative-path-to-source-directory> (e.g. pipenv install -e . or pipenv install -e src).

Other Tools for Application Dependency Management

If you find this particular approach to managing application dependencies isn't working well for you or your use case, you may want to explore these other tools and techniques to see if one of them is a better fit:

  • poetry for a tool comparable in scope to pipenv that focuses more directly on use cases where the repository being managed is structured as a Python project with a valid pyproject.toml file (by contrast, pipenv explicitly avoids making the assumption that the application being worked on that's depending on components from PyPI will itself support distribution as a pip-installable Python package).
  • hatch for opinionated coverage of even more steps in the project management workflow (such as incrementing versions, tagging releases, and creating new skeleton projects from project templates)
  • pip-tools to build your own custom workflow from lower level pieces like pip-compile and pip-sync