97

I want to put all the requirements of a repoze Zope2 install in a pip requirements file. Most of the repoze packages don't seem to be on PyPi, but there's an alternative PyPi index for them here. But I can't figure out how to tell pip to use that index together with a requirements file. For single packages, it's easy

pip install zopelib -i http://dist.repoze.org/zope2/2.10/simple/

I tried the following

pip install -r requirements.txt -i http://dist.repoze.org/zope2/2.10/simple/

or in my requirements.txt all kind or permutations of these:

zopelib -i http://dist.repoze.org/zope2/2.10/simple/
zopelib --index http://dist.repoze.org/zope2/2.10/simple/
-i http://dist.repoze.org/zope2/2.10/simple/ zopelib

or (because the documentation says "Note that all these options must be on a line of their own.")

--index http://dist.repoze.org/zope2/2.10/simple/
zopelib

So, what's the correct way of telling pip to use http://dist.repoze.org/zope2/2.10/simple/ as index?

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Benjamin Wohlwend
  • 30,958
  • 11
  • 90
  • 100
  • 4
    You were very close in your last attempt; the full name of this option is `--index-url` not `--index`. – Piotr Dobrogost Oct 13 '14 at 12:33
  • Docs (section `The following options are supported`): https://pip.pypa.io/en/stable/reference/pip_install/?highlight=requirements.txt#requirements-file-format – Dušan Maďar Mar 20 '19 at 08:27

2 Answers2

139

requirements.txt:

-i http://dist.repoze.org/zope2/2.10/simple
zopelib

Example:

$ pip install -r requirements.txt
...
Successfully installed zopelib
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 34
    For those curious, `-i` is short for `--index-url` if anyone prefers to be explicit. – foslock Dec 14 '16 at 19:04
  • 8
    For those who get a rejected connection due to security warning, you may need to add the domain as trusted to the command: `-i http://some.domain.org/simple --trusted-host some.domain.org` – Arne Jan 29 '18 at 14:13
  • 4
    If I have multiple packages from the same repo, do I have to include index URL for each package, or does the place where it is placed in file matters? – kadamb Jun 11 '19 at 10:21
  • @kadamb: It's been awhile since I needed to use such syntax. My guess it is enough to include the option once and I don't know whether the order of options matters -- you could test it and post it as your own StackOverflow question & answer (or just leave a comment here, to help future visitors with a similar problem) [The docs might help](https://pip.pypa.io/en/stable/reference/pip_install/?highlight=requirements.txt#requirements-file-format) – jfs Jun 11 '19 at 16:08
  • any way to make this work with using the requirements file lines in setup.py? – tkruse Aug 20 '19 at 05:30
  • @tkruse there is setuptools' `dependency_links` but usually requirements in the setup.py are "abstract" and therefore such details should perhaps be reserved for "concrete" requirements used by pip. – jfs Aug 21 '19 at 11:31
67

Add an extra index location to the requirements file just before the package/project name:

--extra-index-url <Extra URLs other than index-url>
<some_project_name>

Alternatively, you may use -i or --index-url <Base URL of the Python Package Index>.

Refer: requirements file format

Marcel Gosselin
  • 4,610
  • 2
  • 31
  • 54
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • 3
    This works for me. With `-i`, `pip install` is restricted to use only one source. – kayochin Jun 14 '20 at 11:36
  • 13
    Use `--index-url` instead of `--extra-index-url` to prevent [dependency confusion attacks](https://medium.com/@alex.birsan/dependency-confusion-4a5d60fec610). – bfontaine Jun 04 '21 at 09:48
  • 4
    @animenon As per the requirements format page ( https://web.archive.org/web/20220128085123/https://pip.pypa.io/en/stable/reference/requirements-file-format/#global-options ), --extra-index-url is a global option which applies to the entire file. Your answer "Add an extra index location...just before the package/project name" implies a per package option. – Shmuel Kamensky Jan 31 '22 at 12:55
  • 2
    @bfontaine How can I use `--index-url` for my custom packages but search on PyPI for everything else in a `requirements.txt` file? – Rafael-WO Sep 30 '22 at 07:21
  • 3
    @Rafael-WO IIRC you can’t do it in one command; you have to use two files: one with your custom requirements, and one with the others. For example: `pip --index-url … custom-requirements.txt` and then `pip install -r other-requirements.txt`. – bfontaine Sep 30 '22 at 07:59