16

Is there a way to tell the tox test automation tool to use the PyPI mirrors while installing all packages (explicit testing dependencies in tox.ini and dependencies from setup.py)?

For example, pip install has a very useful --use-mirrors option that adds mirrors to the list of package servers.

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Andrey Vlasovskikh
  • 16,489
  • 7
  • 44
  • 62

4 Answers4

16

Pip also can be configured using environment variables, which tox lets you set in the configuration:

setenv =
    PIP_USE_MIRRORS=...

Note that --use-mirrors has been deprecated; instead, you can set the PIP_INDEX_URL or PIP_EXTRA_INDEX_URL environment variables, representing the --index-url and --extra-index-url command-line options.

For example:

setenv = 
    PIP_EXTRA_INDEX_URL=http://example.org/index

would add http://example.org/index as an alternative index server, used if the main index doesn't have a package.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    [`indexserver`](https://tox.readthedocs.io/en/latest/config.html#conf-indexserver) is now deprecated. See a [discussion on the topic](https://github.com/tox-dev/tox/issues/1357). The doc has been updated in this [_PR_](https://github.com/tox-dev/tox/pull/1728). See ["_Using a different default PyPI URL_"](https://tox.readthedocs.io/en/latest/example/basic.html#using-a-different-default-pypi-url) and ["_Installing dependencies from multiple PyPI servers_"](https://tox.readthedocs.io/en/latest/example/basic.html#installing-dependencies-from-multiple-pypi-servers) for the current recommendation. – sinoroc Dec 20 '20 at 12:48
  • @sinoroc: thanks for the heads-up, I've removed indexserver altogether. – Martijn Pieters Dec 24 '20 at 16:27
  • 1
    I want to add that `setenv` should be under each environment, not under `[tox]`. – Keunwoo Choi Jan 22 '21 at 23:16
13

Since indexserver is deprecated and would be removed and --use-mirrors is deprecated as well, you can use install_command (in your environment section):

[testenv:my_env]
install_command=pip install --index-url=https://my.index-mirror.com --trusted-host=my.index-mirror.com {opts} {packages}
Peter K
  • 1,959
  • 1
  • 16
  • 22
  • 1
    See ["_Using a different default PyPI URL_"](https://tox.readthedocs.io/en/latest/example/basic.html#using-a-different-default-pypi-url) and ["_Installing dependencies from multiple PyPI servers_"](https://tox.readthedocs.io/en/latest/example/basic.html#installing-dependencies-from-multiple-pypi-servers) for the current recommendations. – sinoroc Dec 20 '20 at 12:50
7

Tox can be configured to install dependencies and packages from a different default PyPI server:

  • as tox command line argument

    tox -i http://pypi.my-alternative-index.org
    
  • using tox.ini

    [tox]
    indexserver =
        default = http://pypi.my-alternative-index.org
    

Link to Tox documentation on using a different default PyPI url

Max
  • 130
  • 3
  • 10
  • 1
    [_`indexserver`_ is deprecated](https://tox.readthedocs.io/en/latest/config.html#conf-indexserver). – sinoroc Dec 28 '20 at 10:39
2

From the pip docs:

pip’s command line options can be set with environment variables using the format PIP_<UPPER_LONG_NAME> . Dashes (-) have to be replaced with underscores (_).

Source: https://pip.pypa.io/en/stable/user_guide/#environment-variables

This translates into setting the following environment variables:

PIP_INDEX_URL=https://server1/pypi/simple
PIP_EXTRA_INDEX_URL=https://server2/pypi/simple

So, with tox, you can e.g. set:

[testenv]
setenv = 
    PIP_INDEX_URL=https://server1/pypi/simple
    PIP_EXTRA_INDEX_URL=https://server2/pypi/simple

However, you can only specify one extra index url with PIP_EXTRA_INDEX_URL. If you need multiple ones, pip recommends appending multiple --extra-index-url <URL> after the pip command so if you need more than one extra index URL, you could possibly utilize tox's install_command:

[testenv]
install_command =
    python -m pip install {opts} {packages} --extra-index-url <URL1> --extra-index-url <URL2>

fredrik
  • 9,631
  • 16
  • 72
  • 132
  • 1
    It is apparently valid to specify multiple space-separated values per _pip_ environment variable: `PIP_EXTRA_INDEX_URL="https://charlie.dev/simple https://delta.dev/simple"`. -- https://pip.pypa.io/en/stable/user_guide/#environment-variables – sinoroc Dec 20 '20 at 13:06