在 tox 中临时使用候选 python 版本

temporary use of release candidate python version in tox

我正在使用 tox 来测试我的 python 包。现在 python 2.7.13 有一个候选版本,我如何使用 tox 测试这个版本而不立即替换我当前的 python 2.7 版本以供正常使用?

我知道如何在创建 virtualenv 时为 python 指定路径,然后我可以安装 运行 py.test。 Tox 构建了自己的 virtualenv,除了选择 py27py26py35.

之外,我不知道如何影响安装的 python 版本

我该如何解决这个问题?

您可以使用 PATH 影响它,方法是在调用 tox 之前扩展您的 PATH(至少在 Linux 上,可能不会在 Windows 上)。

为了获得更细粒度的控制并让它在 Windows 上工作(AFAIK 你不能像在 Unix/Linux 上那样为单个命令使用设置环境变量),你可以使用tox 扩展包 tox-globinterpreter(免责声明我是该包的作者),您可以使用 pip.

安装

该软件包向 tox 添加了一个 --scan 选项,它写出从 py27py36 等到 python 可执行文件的显式映射。

例如我愿意:

tox --scan /opt/python/{2,3}.?/bin/python  /opt/python/pypy2/bin/pypy

在我基于 Linux 的系统上,其中 /opt/python/X.Y 是 link 到最新的 已发布 Python 版本安装在 /opt/python/X.Y.Z(或最新版本,如 3.6 等开发版本),输出为:

interpreters:
python2.6 /opt/python/2.6/bin/python
python2.7 /opt/python/2.7/bin/python
python3.2 /opt/python/3.2/bin/python
python3.3 /opt/python/3.3/bin/python
python3.4 /opt/python/3.4/bin/python
python3.5 /opt/python/3.5/bin/python
python3.6 /opt/python/3.6/bin/python
pypy /opt/python/pypy2/bin/pypy

当我将扫描更改为:

tox --scan /opt/python/2.6/bin/python /opt/python/2.7.13*/bin/python \
    /opt/python/3.?/bin/python /opt/python/pypy2/bin/pypy

我得到:

interpreters:
python2.6 /opt/python/2.6/bin/python
python2.7 /opt/python/2.7.13rc1/bin/python
python3.2 /opt/python/3.2/bin/python
python3.3 /opt/python/3.3/bin/python
python3.4 /opt/python/3.4/bin/python
python3.5 /opt/python/3.5/bin/python
python3.6 /opt/python/3.6/bin/python
pypy /opt/python/pypy2/bin/pypy

并且 tox -e py27 将使用 2.7.13 候选版本。如果来回切换,您可以保存配置文件(在 ~/.config/tox%APPDATA%\tox 中),但重新运行扫描非常快,所以我会制作一个 shell script/alias/batch 文件为此。