1099

Trying to pip install a repo's specific branch. Google tells me to

pip install https://github.com/user/repo.git@branch

The branch's name is issue/34/oscar-0.6 so I did pip install https://github.com/tangentlabs/django-oscar-paypal.git@/issue/34/oscar-0.6 but its returning a 404.

How do I install this branch?

frnhr
  • 12,354
  • 9
  • 63
  • 90
goh
  • 27,631
  • 28
  • 89
  • 151
  • 4
    to me your suggestion work e.g. `pip install https://github.com/user/repo.git@branch` doing `pip install -U git+https://github.com/moskomule/anatome.git@dev` worked. Perhaps remove the extra `/`? – Charlie Parker Nov 12 '21 at 16:36

8 Answers8

1452

Prepend the url prefix git+ (See VCS Support):

pip install git+https://github.com/tangentlabs/django-oscar-paypal.git@issue/34/oscar-0.6

And specify the branch name without the leading /.

0x90
  • 39,472
  • 36
  • 165
  • 245
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 14
    is it mendatory to specify branch or commit by `@` ? – eugene Jan 19 '16 at 09:07
  • 25
    @eugene, No, `@` and parts after that is optional. – falsetru Jan 19 '16 at 09:20
  • @SalahAdDin, I don't understand what you mean by `tree`. The given command in the answer works fine : http://i.imgur.com/5q9F0CC.png – falsetru Mar 10 '17 at 13:33
  • 16
    Is for branches, for example: `pip install -U git+https://github.com/danreeves/wagtailgmaps@3.0.0` – SalahAdDin Mar 10 '17 at 13:34
  • @SalahAdDin, The command you gave works fine. http://i.imgur.com/YtzNvMF.png Could you explain more? – falsetru Mar 10 '17 at 13:42
  • I've done this, but there's only 6 text files in the `python/lib/site-packages/my_module` folder. I'd expect there to be the full source code straight from the github repo. – James T. Dec 19 '17 at 22:10
  • On a related note, how do you switch between branches i.e., dev back to master branch? – Kambiz Apr 20 '18 at 18:45
  • @Kambiz, `issue/34/oscar-0.6` is a branch name as mentioned in the question. Replace that with `master` / `dev` as you want. `pip install git+https://github.com/tangentlabs/django-oscar-paypal.git@master` – falsetru Apr 20 '18 at 22:34
  • Any idea why I am getting ` Command python setup.py egg_info failed with error code 1 ` ? – qwerty Jun 25 '18 at 19:01
  • Before switching branches, is it recommended to remove the package ... or do anything else before switching? – rickhg12hs Oct 01 '18 at 13:48
  • @rickhg12hs, `pip install ...` will remove package before update; I don't do anything. Is there anything in mind? – falsetru Oct 01 '18 at 15:06
  • I want to install the master branch of [jupyter console](https://github.com/jupyter/jupyter_console) and then switch back to normal [pypi jupyter console](https://pypi.org/project/jupyter_console/) when it catches up. I am just wondering about gotchas lurking beneath the surface. – rickhg12hs Oct 01 '18 at 15:26
  • 19
    You can also put something like this `git+https://github.com/adiralashiva8/robotframework-metrics@v3.1.4` into your requirements.txt and then install with `pip install -r requirements.txt`. This will install Tag v3.1.4 from master branch. – Wlad Aug 07 '19 at 14:21
  • After the first time, is there a precise way to tell `pip` to do a `git pull` to install the latest version of the branch? – CivFan Nov 02 '20 at 23:23
  • quotes may be required in some cases - in particular if you are installing a certain package from a subdirectory (or adding any additional url params), like : `pip install "git+https://github.com/tangentlabs/django-oscar-paypal.git#egg=someName&subdirectory=someSubDirectory"` – rocksteady Mar 12 '21 at 21:24
  • Is it normal that after doing this type of install, in `pip list` I am unable to see that a particular package is installed from GiHub, whereas if a package is installed from a local directory, that information is displayed. – MadPhysicist May 20 '21 at 08:56
  • @MadPhysicist Do you mean `pip freeze`? https://imgur.com/PlEF9e3 – falsetru May 20 '21 at 10:42
  • @falsetru I am not interested in `pip freeze`. I was wondering why `pip list` indicates which packages were installed from local directories but fails to do so for repository packages. – MadPhysicist May 20 '21 at 12:14
  • if i do this with the normal package already installed, how do i import the branch specific version in my python program? – Nick Nelson Mar 22 '22 at 20:08
  • .git necessary? – Smart Manoj Apr 23 '22 at 12:52
  • 1
    @SmartManoj, `.git` in URL? It originates from the url shown on github clone button click. – falsetru Apr 24 '22 at 04:15
  • Just copied URL from address bar and it's worked – Smart Manoj Apr 24 '22 at 05:27
431

Using pip with git+ to clone a repository can be extremely slow (test with https://github.com/django/django@stable/1.6.x for example, it will take a few minutes). The fastest thing I've found, which works with GitHub and BitBucket, is:

pip install https://github.com/user/repository/archive/branch.zip

which becomes for Django master:

pip install https://github.com/django/django/archive/master.zip

for Django stable/1.7.x:

pip install https://github.com/django/django/archive/stable/1.7.x.zip

With BitBucket it's about the same predictable pattern:

pip install https://bitbucket.org/izi/django-admin-tools/get/default.zip

Here, the master branch is generally named default. This will make your requirements.txt installing much faster.

Some other answers mention variations required when placing the package to be installed into your requirements.txt. Note that with this archive syntax, the leading -e and trailing #egg=blah-blah are not required, and you can just simply paste the URL, so your requirements.txt looks like:

https://github.com/user/repository/archive/branch.zip
Teymour
  • 1,832
  • 1
  • 13
  • 34
Steve K
  • 10,879
  • 4
  • 39
  • 39
  • 43
    **Note:** from Django 1.9 on, Django ships with a file that has a [unicode filename](https://github.com/django/django/commit/bd059e3f8c6311dcaf8afe5e29ef373f7f84cf26). The zip extractor used by pip chokes on that. An easy workaround is to replace `.zip` with `.tar.gz`, as the tar extractor works. – spectras Jul 03 '16 at 11:56
  • 7
    I wonder if pip could pass `--depth 0` when cloning to make it more efficient (the entire git history is not needed to install a snapshot for pip). https://www.git-scm.com/docs/git-clone – cs01 Mar 31 '17 at 18:15
  • 10
    This also works for commit hashes! `pip install https://github.com/django/django/archive/ebaa08b.zip` – Fush Apr 12 '17 at 03:31
  • 7
    Thanks for pointing out the speed difference. I did not test and compare them, but I believe the speed difference does exist, because installing from a branch would still result in downloading the entire repo history, while installing from a `.zip` (or `.tar.gz`) would result in downloading just a snapshot of the repo. – RayLuo Jun 02 '17 at 20:52
  • 2
    Is it possible to install extras using this syntax? For instance, I'm trying to install https://github.com/apache/incubator-airflow @ master (the normal PyPI package is apache-airflow) to work with an unreleased version. I'd like to convert the call `pip install apache-airflow[crypto, slack]` to install these extras with the archive version. I tried `pip install https://github.com/apache/incubator-airflow/archive/master.zip[crypto, slack]` but this breaks the URL and installation. – Taylor D. Edmiston Aug 08 '17 at 01:21
  • 3
    Okay, so it seems to be possible when running pip install directly and just requires setting the egg name explicitly - `pip install https://github.com/apache/incubator-airflow/archive/master.zip#egg=airflow[crypto,slack]`. Also the spaces between extras in my previous comment are a mistake. Note that however this syntax with extras _does not_ seem to work in a requirements.txt file. – Taylor D. Edmiston Aug 08 '17 at 01:46
  • 2
    Note that despite `pipenv` is rather similar to `pip`, using archives with `pipenv` will not resolve dependencies. – Eugene Pakhomov Jan 02 '18 at 09:11
  • FWIW using pip 22 the accepted answer was not any slower than this method, and doesn't appear to clone the entire git history of the repo – vtnate Feb 11 '22 at 18:24
  • Found the .zip install to be much faster when using repos that contain multiple levels of submodules. Skipping the recursive submodule downloads offers a speed advantage , but may also break dependencies. – Jos Verlinde Feb 27 '22 at 14:55
  • There are reports that recursive dependency installation doesn't work when you use `pipenv` for archives, but [it works for me](https://stackoverflow.com/a/75213065/241211). I think it was [fixed in 2018-09](https://github.com/pypa/pipenv/commit/683df2f8ad73e53e9e50364a9cfeb7493a7f1552). – Michael Jan 26 '23 at 21:53
176

Instructions to install from private repo using ssh credentials:

$ pip install git+ssh://git@github.com/myuser/foo.git@my_version

To install a package from a subdirectory, say stackoverflow

$ pip install git+ssh://git@github.com/myuser/foo.git@my_version#subdirectory=stackoverflow

https://pip.pypa.io/en/stable/topics/vcs-support/

vikas027
  • 5,282
  • 4
  • 39
  • 51
Jaakko
  • 4,674
  • 2
  • 26
  • 20
58

Just to add an extra, if you want to install it in your pip file it can be added like this:

-e git+https://github.com/tangentlabs/django-oscar-paypal.git@issue/34/oscar-0.6#egg=django-oscar-paypal

It will be saved as an egg though.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Hassek
  • 8,715
  • 6
  • 47
  • 59
  • 7
    Better to use it without the `-e`. See: http://stackoverflow.com/a/34518202/451710 – Eyal Levin Jan 01 '16 at 19:23
  • 5
    Thanks for the comment, very interesting. I think people tend to use the `-e` flag to avoid any possible conflict with an already existent package. I guess is a matter of choice – Hassek Jan 04 '16 at 17:27
  • 5
    And if you want "extras", append them in he fragment, like that: ``-e git+https://github.com/tangentlabs/django-oscar-paypal.git@issue/34/oscar-0.6#egg=django-oscar-paypal[PDF]`` – ankostis Jan 20 '17 at 18:29
  • Note that the `-e` does not actually seem to be required. – Taylor D. Edmiston Aug 08 '17 at 01:54
  • For some reason, it doesn't work for me without the `-e` – Kurt Bourbaki Jan 12 '18 at 11:00
  • @EyalLevin Better to use it **WITH** the `-e`. See https://stackoverflow.com/questions/16584552/how-to-state-in-requirements-txt-a-direct-github-source/34518202#comment76338057_34518202 – Phani Rithvij Jul 11 '21 at 08:06
49

This worked like charm:

pip3 install git+https://github.com/deepak1725/fabric8-analytics-worker.git@develop
Field Value
User deepak1725
Repository fabric8-analytics-worker
Branch develop
Michael
  • 8,362
  • 6
  • 61
  • 88
Deepak Sharma
  • 1,401
  • 19
  • 21
11

You used the egg files install procedure. This procedure supports installing over git, git+http, git+https, git+ssh, git+git and git+file. Some of these are mentioned in other answers.

It's good. You can use branches, tags, or hashes to install.

Steve_K noted it can be slow to install with "git+" and proposed installing via zip file:

pip install https://github.com/user/repository/archive/branch.zip

Alternatively, I suggest you may install using the .whl file if this exists.

pip install https://github.com/user/repository/archive/branch.whl

It's pretty new format, newer than egg files. It requires wheel and setuptools>=0.8 packages. You can find more in the documentation.

Michael
  • 8,362
  • 6
  • 61
  • 88
prosti
  • 42,291
  • 14
  • 186
  • 151
10

to me your suggestion from question work e.g.

pip install https://github.com/user/repo.git@branch

translating concretely to doing

pip install -U git+https://github.com/moskomule/anatome.git@dev

worked. Perhaps remove the extra / is redundant. My output:

(original_anatome_env) brando~/ultimate-anatome ❯ pip install -U git+https://github.com/moskomule/anatome.git@dev
Collecting git+https://github.com/moskomule/anatome.git@dev
  Cloning https://github.com/moskomule/anatome.git (to revision dev) to /private/var/folders/x4/0xq0brj57xz3dbhbmblypbm00000gr/T/pip-req-build-62d_ghd2
  Running command git clone -q https://github.com/moskomule/anatome.git /private/var/folders/x4/0xq0brj57xz3dbhbmblypbm00000gr/T/pip-req-build-62d_ghd2
  Running command git checkout -b dev --track origin/dev
  Switched to a new branch 'dev'
  Branch 'dev' set up to track remote branch 'dev' from 'origin'.
  Resolved https://github.com/moskomule/anatome.git to commit 4b576e51cb1824a57ea04974e0f92b5a6143294d
Requirement already satisfied: torch>=1.10.0 in /Users/brando/anaconda3/envs/metalearning/envs/original_anatome_env/lib/python3.9/site-packages (from anatome==0.0.6) (1.10.0)
Requirement already satisfied: torchvision>=0.11.1 in /Users/brando/anaconda3/envs/metalearning/envs/original_anatome_env/lib/python3.9/site-packages (from anatome==0.0.6) (0.11.1)
Requirement already satisfied: typing-extensions in /Users/brando/anaconda3/envs/metalearning/envs/original_anatome_env/lib/python3.9/site-packages (from torch>=1.10.0->anatome==0.0.6) (3.10.0.2)
Requirement already satisfied: pillow!=8.3.0,>=5.3.0 in /Users/brando/anaconda3/envs/metalearning/envs/original_anatome_env/lib/python3.9/site-packages (from torchvision>=0.11.1->anatome==0.0.6) (8.4.0)
Requirement already satisfied: numpy in /Users/brando/anaconda3/envs/metalearning/envs/original_anatome_env/lib/python3.9/site-packages (from torchvision>=0.11.1->anatome==0.0.6) (1.21.4)
Building wheels for collected packages: anatome
  Building wheel for anatome (setup.py) ... done
  Created wheel for anatome: filename=anatome-0.0.6-py3-none-any.whl size=10167 sha256=63b12a36f33deb8313bfe7756be60bd08915b8ba36711be47e292b590df70f61
  Stored in directory: /private/var/folders/x4/0xq0brj57xz3dbhbmblypbm00000gr/T/pip-ephem-wheel-cache-rde_ngug/wheels/19/e4/be/01479e8cba62ae8cdcd501cd3bf49e199f2bb94732a6a1b006
Successfully built anatome
Installing collected packages: anatome
  Attempting uninstall: anatome
    Found existing installation: anatome 0.0.5
    Uninstalling anatome-0.0.5:
      Successfully uninstalled anatome-0.0.5
Successfully installed anatome-0.0.6

0.6.0 is the dev branch version number and 0.5.0 is the master, so it worked!

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
-2

For windows & pycharm setup:

If you are using pycharm and If you want to use pip3 install git+https://github.com/...

  • firstly, you should download git from https://git-scm.com/downloads
  • then restart pycharm
  • and you can use pycharm terminal to install what you want

enter image description here

Ekremus
  • 237
  • 2
  • 4
  • 7
    Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee Sep 06 '21 at 16:12