vpip.pip_api

pip command API.

class vpip.pip_api.InspectGraph(installed)

Bases: object

The graph of installed packages.

packages: dict[NormalizedName, Package]

A dictionary of installed packages.

class vpip.pip_api.Package(data)

Bases: object

Package information. You can get this object by get_pkg_info().

property entry_points: str

Text content of entry_points.txt. Lazily loaded.

metadata_location: str

Metadata location

name: str

Package name.

normalized_name: str

Normalized package name.

required_by: set[Package]

Packages that require this

requires: set[Package]

Package dependencies

version: str

Package version.

vpip.pip_api.create_ns_from_dict(d)

Create a namespace object from a dict.

Parameters:

d (dict) – Dictionary.

Return type:

argparse.Namespace

vpip.pip_api.execute_pip(cmd, capture=False)

Run pip command.

Parameters:
  • cmd (str) – pip command. It would be prefixed with python -m pip.

  • capture (bool) – Whether to capture output.

vpip.pip_api.freeze(include: Container[str] | None = None, exclude: Container[str] | None = None) List[str]

List installed packages in pip freeze format (my_pkg==1.2.3).

Parameters:
  • include – If defined, only returns specified packages.

  • exclude – If defined, exclude specified packages.

vpip.pip_api.get_compatible_version(version)

Return the compatible version.

Parameters:

version (str) – Version string.

Returns:

The compatible version which could be used as ~={compatible_version}.

Return type:

str

Suppose the version string is x.y.z:

  • If x is zero then return x.y.z.

  • Otherwise return x.y.

vpip.pip_api.get_pkg_info(name: str, cache=True) Package

Get package information.

vpip.pip_api.get_pkg_infos(names: list[str], cache=True) Iterator[Package]

Get multiple packages information.

vpip.pip_api.inspect() InspectGraph

Inspect packages. The result is cached according to the active virtual environment.

vpip.pip_api.install(packages: List[str], install_scripts: str | None = None, upgrade: bool = False, latest: bool = False, deps: bool = True) List[str]

Install packages and return a list of collected package names.

Parameters:
  • packages – A list of package name, which may include the version specifier. It can also be a URL.

  • install_scripts – Install scripts to a different folder. It uses the --install-option="--install-scripts=..." pip option.

  • upgrade – Upgrade package.

  • latest – Whether upgrade to the latest version. Otherwise upgrade to the compatible version. This option has no effect if package includes specifiers.

  • deps – Whether to install dependencies.

vpip.pip_api.install_requirements(file='requirements.txt')

Install requirements.txt file.

vpip.pip_api.list_(not_required=False, format='json')

List installed packages.

Return type:

list[argparse.Namespace]

vpip.pip_api.show(packages, verbose=False)

Get package information.

Parameters:
  • packages (list[str]) – A list of package name.

  • verbose (bool) – Whether to return verbose info.

Returns:

A list of namespace objects holding the package information.

Return type:

list[Namespace]

This function uses pip show under the hood. Property name is generated by case_conversion.snakecase().

vpip.pip_api.uninstall(packages)

Uninstall packages.

Parameters:

package (list[str]) – Package name.