27.15.1.14. packaging.install — Installation tools

Packaging provides a set of tools to deal with downloads and installation of distributions. Their role is to download the distribution from indexes, resolve the dependencies, and provide a safe way to install distributions. An operation that fails will cleanly roll back, not leave half-installed distributions on the system. Here’s the basic process followed:

  1. Move all distributions that will be removed to a temporary location.
  2. Install all the distributions that will be installed in a temporary location.
  3. If the installation fails, move the saved distributions back to their location and delete the installed distributions.
  4. Otherwise, move the installed distributions to the right location and delete the temporary locations.

This is a higher-level module built on packaging.database and packaging.pypi.

27.15.1.14.1. Public functions

packaging.install.get_infos(requirements, index=None, installed=None, prefer_final=True)

Return information about what’s going to be installed and upgraded. requirements is a string containing the requirements for this project, for example 'FooBar 1.1' or 'BarBaz (<1.2)'.

If you want to use another index than the main PyPI, give its URI as index argument.

installed is a list of already installed distributions used to find satisfied dependencies, obsoleted distributions and eventual conflicts.

By default, alpha, beta and candidate versions are not picked up. Set prefer_final to false to accept them too.

The results are returned in a dictionary containing all the information needed to perform installation of the requirements with the install_from_infos() function:

>>> get_install_info("FooBar (<=1.2)")
{'install': [<FooBar 1.1>], 'remove': [], 'conflict': []}
packaging.install.install(project)
packaging.install.install_dists(dists, path, paths=None)

Safely install all distributions provided in dists into path. paths is a list of paths where already-installed distributions will be looked for to find satisfied dependencies and conflicts (default: sys.path). Returns a list of installed dists.

packaging.install.install_from_infos(install_path=None, install=[], remove=[], conflicts=[], paths=None)

Safely install and remove given distributions. This function is designed to work with the return value of get_infos(): install, remove and conflicts should be list of distributions returned by get_infos(). If install is not empty, install_path must be given to specify the path where the distributions should be installed. paths is a list of paths where already-installed distributions will be looked for (default: sys.path).

This function is a very basic installer; if conflicts is not empty, the system will be in a conflicting state after the function completes. It is a building block for more sophisticated installers with conflict resolution systems.

packaging.install.install_local_project(path)

Install a distribution from a source directory, which must contain either a Packaging-compliant setup.cfg file or a legacy Distutils setup.py script (in which case Distutils will be used under the hood to perform the installation).

packaging.install.remove(project_name, paths=None, auto_confirm=True)

Remove one distribution from the system.