Explain Codes LogoExplain Codes Logo

How to install Python package from GitHub?

python
pip-install
github-packages
package-management
Anton ShumikhinbyAnton Shumikhin·Nov 22, 2024
TLDR

Run the following command to install a Python package directly from GitHub:

pip install git+https://github.com/username/repo.git

Switch https://github.com/username/repo.git with the repository's URL on GitHub. To install a specific branch or commit, use this command:

pip install git+https://github.com/username/repo.git@branch_or_commit

Substitute branch_or_commit with your desired branch name or commit hash. Now you're git-ting somewhere!

When the package is not configured for pip, clone the repo and then manually install:

git clone https://github.com/username/repo.git cd repo python setup.py install

Getting down to the nitty-gritty

Before you pip install all the things, let's look at some fine details and important caveats.

Package metadata essentials

Modern versions of pip are designed to track package metadata efficiently, but it needs a little help when metadata files like PKG-INFO are unavailable. For these cases, add #egg=projectname at the end:

pip install git+https://github.com/username/repo.git#egg=projectname

Stay ahead of the curve by regularly updating your pip version.

Doing battle with errors

Come across a permissions error? Don't pip your pants! You might need to use sudo:

sudo pip install git+https://github.com/username/repo.git

Remember though, using sudo can invite security risks and cause conflict with system packages. Instead, use the --user flag or better yet, stick to a virtual environment.

Advanced GitHubbery and common hiccups

Here's how you can take your pip install game to the next level and address common issues.

Choosing the right version

Installing a package from a specific release or branch is as easy as appending an @ followed by the branch name or tag:

pip install git+https://github.com/username/[email protected] # I'll take one version 1.2.3, please!

Remember to replace 1.2.3 with the desired tag or branch name.

When 'pip install' stalls

If pip install fails due to the repository's structure or missing metadata, clone the repo and follow the instructions detailed in the README or INSTALL files.

Beyond 'pip install'

Keep your eyes peeled for these situations:

  • Requirements Files: If dependencies are listed in a requirements.txt file, run pip install -r requirements.txt after cloning.
  • Submodules: Make sure to initialize any Git submodules using git submodule update --init after cloning.
  • Python Version: Check if the package is compatible with your Python version. Some rely heavily on features specific to Python 2 or 3.