我正在使用 tox
来测试我的 python 包。现在 python 2.7.13 有一个候选版本,我如何使用 tox
测试这个版本而不立即替换我当前的 python 2.7 版本以供正常使用?
我知道如何在创建 virtualenv
时为 python 指定路径,然后我可以安装并运行 py.test
。 Tox 构建了自己的 virtualenv,除了选择 py27
、py26
、py35
之外,我不知道如何影响安装的 python 版本。
我该如何解决这个问题?
最佳答案
您可以使用 PATH 影响它,方法是在调用 tox 之前扩展您的 PATH(至少在 Linux 上,可能不会在 Windows 上)。
为了获得更细粒度的控制并让它在 Windows 上工作(据我所知,你不能像在 Unix/Linux 上那样为单个命令使用设置环境变量),你可以使用 tox 扩展包 tox -globinterpreter
(免责声明我是该软件包的作者),您可以使用 pip
安装它。
该软件包向 tox
添加了一个 --scan
选项,它从 py27
、py36
中写出一个显式映射> 等到 python 可执行文件。
例如我这样做:
tox --scan /opt/python/{2,3}.?/bin/python /opt/python/pypy2/bin/pypy
在我基于 Linux 的系统上,其中/opt/python/X.Y 是指向安装在/opt/python/X.Y.Z 中的最新已发布 Python 版本的链接(或者开发时的最新版本版本如 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 脚本/别名/批处理文件。
关于python-2.7 - 在 tox 中临时使用发布候选 python 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40960613/