When you need to install a Python package directly from a GitHub repository, especially when the repository shares its name with an existing package on PyPI, you can use pip with the repository's URL. Here's how to do it:
-
Install Directly from the GitHub Repository:
Use the following command to install the package directly from its GitHub repository:
pip install git+https://github.com/username/repository.git
Replace username with the GitHub account name and repository with the repository name. This method ensures that you're installing the package from the specified repository, regardless of any existing packages with the same name on PyPI.
-
Specify a Particular Branch, Tag, or Commit:
If you want to install a specific version of the package, you can append a branch name, tag, or commit hash to the URL:
-
Branch:
pip install git+https://github.com/username/repository.git@branch_name
-
Tag:
pip install git+https://github.com/username/repository.git@v1.0
-
Commit:
pip install git+https://github.com/username/repository.git@commit_hash
Replace branch_name, v1.0, or commit_hash with the appropriate reference.
-
Install from a Subdirectory:
If the Python package is located within a subdirectory of the repository, you can specify the subdirectory path:
pip install git+https://github.com/username/repository.git#subdirectory=subdir_path
Replace subdir_path with the path to the subdirectory containing the setup.py file.
-
Use Editable Mode for Development:
If you're actively developing the package and want changes to reflect immediately without reinstalling, use the -e flag:
pip install -e git+https://github.com/username/repository.git#egg=package_name
Replace package_name with the name of the package as defined in its setup.py.
Note: Ensure that you have git installed on your system, as pip uses it to clone repositories. Additionally, if the repository is private, you'll need the appropriate access permissions and may need to configure authentication methods such as SSH keys or personal access tokens.
By following these methods, you can install Python packages directly from GitHub repositories, even when they share names with existing PyPI packages.