How can I specify a custom Git branch in my Pipfile?

Question:

It is possible to specify in Pipfile packages from custom Git repository.
But I cannot find comprehensive documentation on how to specify the concrete branch or a commit to be used for installation.

Is there a complete reference on how to use Git URLs for python packages in the Pipfile that are supported by the pipenv for specifying custom branches, versions, or commits?

It would be really nice to have it with equivalent pipenv command line arguments.

Asked By: kuza

||

Answers:

Here is some documentation on the GitHub repo for pipenv:

You can install packages with pipenv from git and other version
control systems using URLs formatted according to the following rule:

<vcs_type>+<scheme>://<location>/<user_or_organization>/<repository>@<branch_or_tag>#egg=<package_name>

So, for example:

[packages]
requests = {git = "https://github.com/requests/requests.git", editable = true, ref = "v2.20.1"}

You can generate a Pipfile using the command line. The Pipfile above was generated with:

pipenv install -e git+https://github.com/requests/[email protected]#egg=requests

The documentation for pip goes into more detail.

Answered By: Alex W

pip package archives

In addition to Alex’s excellent answer, you can also use pip‘s "archive" format on a zipped copy of the repository. Several code hosts automatically provide zip files at predictable URLs.

Why would I do this?

The VCS installation works perfectly fine. This alternative is convenient if the machine you’re installing on does not have Git, like a "slim" container image. Previous reports suggested that this method was faster than Git, but they seem comparable now.

Examples

Replace the tokens with the values you want in the code samples below.

Field Description
<package> The name of the package. (Match the package’s name from its setup.py!)
<user> The repository owner
<repo> The repository name
<refname> Branch, tag, or commit SHA. Bitbucket can also use default for the default branch.

Pipfile

[packages]
<package> = {file = "https://github.com/<user>/<repo>/archive/<refname>.zip"}
<package> = {file = "https://bitbucket.org/<user>/<repo>/get/<refname>.zip"}
<package> = {file = "https://codeberg.org/<user>/<repo>/archive/<refname>.zip"}
<package> = {file = "https://gitlab.com/<user>/<repo>/-/archive/<refname>/<repo>-<refname>.zip"}

CLI installation

pipenv install https://github.com/<user>/<repo>/archive/<refname>.zip
pipenv install https://bitbucket.org/<user>/<repo>/get/<refname>.zip
pipenv install https://codeberg.org/<user>/<repo>/archive/<refname>.zip
pipenv install https://gitlab.com/<user>/<repo>/-/archive/<refname>/<repo>-<refname>.zip

Warning

There are reports from 2017 and 2018 that unlike pip, pipenv will not recursively install dependencies from zips. Dependencies are working fine for me, though. This commit from September 2018 is my best guess for when things changed, but if you know otherwise, please comment or edit.

Answered By: Michael
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.