简体   繁体   English

如何针对不同的 Python *patch* 版本测试库?

[英]How to test a library against different Python *patch* versions?

I'm writing a library and want to test against different Python patch versions, like 3.7.1, 3.7.2, etc我正在编写一个库并希望针对不同的 Python 补丁版本进行测试,例如 3.7.1、3.7.2 等

I've been using tox for a long time, however, according to this answer , it doesn't really support this kind of usage.我已经使用tox很长时间了,但是,根据这个答案,它并不真正支持这种用法。

Any suggestions?有什么建议?

For a one of check against 3.8.1 (assuming your python3.8 points to 3.8.2) you can use the discover flag对于针对 3.8.1 的检查之一(假设您的 python3.8 指向 3.8.2),您可以使用发现标志

tox --discover /path/to/python3.8.1 -e py38

If you want to define an environment that always uses 3.8.1 you can do that by defining a new tox environment and setting basepython as python3.8.1 .如果您想定义一个始终使用 3.8.1 的环境,您可以通过定义一个新的 tox 环境并将basepython设置为python3.8.1

[testenv:py381]
basepython = python3.8.1

[testenv:py382]
basepython = python3.8.2

Probably the least troublesome (but tedious) way is to install different versions of Python sequentially in a jail or VM and then test your code on it.可能最不麻烦(但乏味)的方法是在监狱或虚拟机中按顺序安装不同版本的 Python,然后在其上测试您的代码。

If you install Python from source on a UNIX-like system, you could try installing them side by side using different prefixes (eg /opt/patch1 , opt/patch2 etc.) And then expliticly run your test with the correct python like /opt/patch1/bin/python3 .如果您在类 UNIX 系统上从源代码安装 Python,您可以尝试使用不同的前缀(例如/opt/patch1opt/patch2等)并排安装它们,然后使用正确的 python明确地运行您的测试,例如/opt/patch1/bin/python3 One caveat;一个警告; I'm not sure if the Python executable would find the correct shared library in this case.在这种情况下,我不确定 Python 可执行文件是否会找到正确的共享库。

The ms-windows installer lets you pick an install location. ms-windows 安装程序允许您选择安装位置。 If you instruct it not to put Python in the PATH and not set up file associations and the like, that might also work.如果您指示它不要将 Python 放在 PATH 中并且不设置文件关联等,那也可能有效。 You would also have to explicitly invoke the correct Python with the full path.您还必须使用完整路径显式调用正确的 Python。

I would go Docker and run it container.我会去 Docker 并运行它的容器。

$ docker run -it --rm -w /opt -v "$PWD:/opt" python:3.4.2 python <script.py>
 -it - interactive mode --rm - remove container after the run -w - working directory inside container -v - map directory $PWD from host to /opt inside container <container> - python:3.4.2 <command> - python script.py

You can see what images are available with the command:您可以使用以下命令查看哪些图像可用:

$ curl -s https://registry.hub.docker.com/v1/repositories/python/tags | \
    jq -r .[].name | grep "^[23][.0-9]*$" | sort -V
2
2.7
2.7.7
2.7.8
2.7.9
2.7.10
2.7.11
2.7.12
2.7.13
2.7.14
2.7.15
2.7.16
3
3.2
3.2.6
3.3
3.3.5
3.3.6
3.3.7
3.4
3.4.1
3.4.2
3.4.3
3.4.4
3.4.5
3.4.6
3.4.7
3.4.8
3.4.9
3.4.10
3.5
3.5.0
3.5.1
3.5.2
3.5.3
3.5.4
3.5.5
3.5.6
3.5.7
3.6
3.6.0
3.6.1
3.6.2
3.6.3
3.6.4
3.6.5
3.6.6
3.6.7
3.6.8
3.6.9
3.7
3.7.0
3.7.1
3.7.2
3.7.3
3.7.4

grep filters out beta and alpha versions, so if you need them - just remove the grep. grep过滤掉 beta 和 alpha 版本,所以如果你需要它们 - 只需删除 grep。

If you need Python version that is not present in the list, you can build docker image with custom Python.如果您需要列表中没有的 Python 版本,您可以使用自定义 Python 构建docker镜像。

take eg Alpine linux (it's really small) https://github.com/docker-library/python/blob/f82205cde8f0a5ffa276103a50d843edced67757/3.7/alpine3.10/Dockerfile例如 Alpine linux(它真的很小) https://github.com/docker-library/python/blob/f82205cde8f0a5ffa276103a50d843edced67757/3.7/alpine3.10/Dockerfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM