[英]How to manage multiple Python versions in Windows 10 to use with Python tox?
I'm trying to use tox
to automate pytest
testing of my project on Python 3.7 and 3.8 but am struggling with how to best set this up.我正在尝试使用
tox
在 Python 3.7 和 3.8 上对我的项目进行pytest
测试自动化,但我正在努力解决如何最好地设置它。 What's the easiest way to get multiple Python versions installed on my Windows 10 machine in order to use them with tox
?在我的 Windows 10 机器上安装多个 Python 版本以便将它们与
tox
一起使用的最简单方法是什么?
If I just manually install them using the official installers how do I set up my environment variables?如果我只是使用官方安装程序手动安装它们,我该如何设置我的环境变量? Because each installation contains
python.exe
, so if I install two versions and add both their paths C:\Program Files\Python37\
and C:\Program Files\Python38\
to my Path environment variable this won't differentiate them... python
will just refer to the first one listed.因为每个安装都包含
python.exe
,所以如果我安装两个版本并添加它们的路径C:\Program Files\Python37\
和C:\Program Files\Python38\
来区分它们... python
将仅指列出的第一个。 So do I need to go and manually rename the python.exe
files to names like python37.exe
and python38.exe
?那么我是否需要 go 并手动将
python.exe
文件重命名为python37.exe
和python38.exe
之类的名称? This all seems very manual and clunky but I can't find any easier way or tutorial about this step.这一切看起来都非常手动和笨重,但我找不到任何更简单的方法或关于这一步的教程。
The tox
documentation just gives a solution using conda, but I'm not using conda and don't want to switch to it just to use tox
. tox
文档只是提供了一个使用 conda 的解决方案,但我没有使用 conda 并且不想切换到它只是为了使用tox
。
If Python version paths and aliases are set up manually, then if someone clones my project to work on a PR and wants to run tests locally, tox would not work correctly if their Python install locations are different or they're on a different OS.如果 Python 版本路径和别名是手动设置的,那么如果有人克隆我的项目以在 PR 上工作并希望在本地运行测试,如果他们的 Python 安装位置不同或它们在不同的操作系统上,则 tox 将无法正常工作。 Is there some standard way to define and set this all up so things are easy and consistent across machines?
是否有一些标准的方法来定义和设置这一切,以便在机器之间保持简单和一致?
If all this is unavoidable, what is an alternative to tox
for consistently testing projects across multiple Python versions that can work on any machine or CI/CD pipeline?如果这一切都是不可避免的,那么在可以在任何机器或 CI/CD 管道上运行的多个 Python 版本中持续测试项目的
tox
替代方案是什么?
tox
has quite some logic implemented to find installed Python interpreters. tox
实现了相当多的逻辑来查找已安装的 Python 解释器。
While the documentation lacks some details (maybe you want to create an issue?), we can still have a look a the source code:虽然文档缺少一些细节(也许你想创建一个问题?),我们仍然可以查看源代码:
@tox.hookimpl
def tox_get_python_executable(envconfig):
spec, path = base_discover(envconfig)
if path is not None:
return path
# second check if the py.exe has it (only for non path specs)
if spec.path is None:
py_exe = locate_via_pep514(spec)
if py_exe is not None:
return py_exe
# third check if the literal base python is on PATH
candidates = [envconfig.basepython]
# fourth check if the name is on PATH
if spec.name is not None and spec.name != envconfig.basepython:
candidates.append(spec.name)
# or check known locations
if spec.major is not None and spec.minor is not None:
if spec.name == "python":
# The standard names are in predictable places.
candidates.append(r"c:\python{}{}\python.exe".format(spec.major, spec.minor))
return check_with_path(candidates, spec)
As you can see, there are five ways to determine the available Python interpreters on a Windows system.如您所见,有五种方法可以确定 Windows 系统上可用的 Python 解释器。
Especially the second one looks promising -it is using the already mentioned Python launcher for Windows, also see https://www.python.org/dev/peps/pep-0514/ Especially the second one looks promising -it is using the already mentioned Python launcher for Windows, also see https://www.python.org/dev/peps/pep-0514/
As far as I understand it, you just need to install your Python interpreters, and they will be automatically discoverable.据我了解,您只需要安装您的 Python 解释器,它们就会自动被发现。
tox
is definitely a very good way to test Python applications against multiple interpreters, both locally and in CI. tox
绝对是针对本地和 CI 中的多个解释器测试 Python 应用程序的非常好的方法。
PS: Yes, it works!! PS:是的,它有效!
I just rdp'ed into a Windows box, installed Python 3.8 and Python 3.9 - just clicked through the default installer, and created the following tox.ini
我刚刚 rdp'ed 到一个 Windows 盒子,安装了 Python 3.8 和
tox.ini
3.9 - 只需单击以下默认安装程序,然后创建。
[tox]
envlist = py38, py39
[testenv]
commands = python -c "print('hello')"
skip_install = true
Both interpreters were detected and both environments got executed.两个解释器都被检测到并且两个环境都被执行了。
(venv) C:\Users\jugmac00\Projects\stackoverflow>tox
py38 create: C:\Users\jugmac00\Projects\stackoverflow\.tox\py38
py38 run-test-pre: PYTHONHASHSEED='296'
py38 run-test: commands[0] | python -c 'print('"'"'hello'"'"')'
hello
py39 create: C:\Users\jugmac00\Projects\stackoverflow\.tox\py39
py39 run-test-pre: PYTHONHASHSEED='296'
py39 run-test: commands[0] | python -c 'print('"'"'hello'"'"')'
hello
_______________________________________________________ summary _______________________________________________________
py38: commands succeeded
py39: commands succeeded
congratulations :)
(venv) C:\Users\jugmac00\Projects\stackoverflow>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.