Earlier in the course, we installed the cowsay package. Unless otherwise specified, pip installs packages from the main index, PyPI.
In most cases, PyPI is the source we need. But there are cases where the package is in an alternative index, for example, an internal corporate one. Sometimes, developers upload packages to GitHub instead of publishing them in any index.
Pip knows how to work with alternative indexes and repositories containing the source code of packages. In this lesson, we'll look at both variations.
Alternative package indexes
If you call the pip install
command with the --index-url <url>
option, pip will look for the package and all its dependencies in the index at the specified URL address. Let's try to install a package from the training index Test PyPI:
Note that the URL address of the index can be in the form of something-something'/'simple
— it is how we should name indexes according to the convention.
Test PyPI contains most of the packages in the main PyPI, but usually, local index holders don't want to keep copies of all versions of all packages.
Usually, holders only store their packages and get all external dependencies from another index, usually an already regular PyPI.
The pip also has an option for this case: you can specify --index-url
in addition to --extra-index-url <url>
. In this configuration, pip looks for each package in the first index. If it doesn't find them, it looks for an additional index. The command as a whole may look like this:
python3 -m pip install --user --index-url https://test.pypi.org/simple --extra-index-url https://pypi.org/simple dogesay
Installing packages from repositories on GitHub
Sometimes you might not want to upload a package to PyPI. For example, when it isn't ready, you need to check and try to install it. In such cases, packages are installed directly from git repositories.
Let's install our tutorial package hexlet-boilerplates/python-package using pip:
This package will only install if you have pip version 20 and above. If you see an error when installing the package, try updating the pip itself with this command:
python3 -m pip install --user --upgrade pip
Instead of the package name, we specify the URL address you would use when cloning the repository, but with the prefix git+
appended. This prefix tells pip that the URL is a Git repository — not necessarily hosted on GitHub.
During the package, pip will call git clone
to clone the repository into a temporary directory. If we want to access a private repository, we enter a username and password. It also works with private GitHub repositories.
In general, pip supports other version control systems besides Git. Managing the installation of packages from these systems is very flexible. For example, you can specify the branches, tags, or commit hashes where the desired version of the located package is. You can read more about this in the documentation.
We haven't covered all possible package sources and briefly discussed the pip install
command. If possible, we recommend you learn this and other pip commands in great detail, as pip is an essential tool for a Python developer.
Recommended materials
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.