简体   繁体   English

如何管理 Windows 10 中的多个 Python 版本以与 Python 一起使用?

[英]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.exepython38.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.

相关问题 在 Windows 中运行多个版本的 Python 和 tox - Run multiple versions of Python with tox in Windows 使用tox在Windows上设置多个python安装 - Set up multiple python installations on windows with tox 当安装了多个版本的python时,tox使用了错误版本的pip - tox uses wrong version of pip when multiple versions of python are installed 如何获得 tox 以测试具有不同 python 版本的“命名”测试环境? - How to get tox to test "named" test environments with different python versions? 如何在OS X 10.10.3中卸载和/或管理多个版本的python - How to uninstall and/or manage multiple versions of python in OS X 10.10.3 如何管理依赖于共享库的多个版本的python项目? - How to manage python project that depends on multiple versions of a shared library? 在不同的 python 补丁版本上运行 tox - run tox on different python patch versions 如何在 Windows 上运行多个 Python 版本 - How to run multiple Python versions on Windows 在Archlinux上管理多个Python版本的“正确方法” - “Proper way” to manage multiple versions of Python on archlinux 带有 tox 的简单 Python 项目的 CircleCI:如何测试多个 Python 环境? - CircleCI for simple Python project with tox: how to test multiple Python environments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM