简体   繁体   English

当我安装了多个 python 版本时,如何为 VSCode 虚拟环境指定 python 版本?

[英]How to specify the python version for VSCode virtual environment, when I have multiple python versions intalled?

I have both 64-bit and 32-bit Python installed.我安装了 64 位和 32 位 Python。 I was trying to create a virtual environment using the 32-bit Python.exe file in VSCode.我试图使用 VSCode 中的 32 位 Python.exe 文件创建虚拟环境。 I selected that version in the Python: Select Interpreter for my workspace (ie the C:\Program Files (x86)\Python37-32\python.exe).我在 Python: Select Interpreter 中为我的工作区选择了该版本(即 C:\Program Files\python.exe.exe)。

I then changed the launch.json file in the workspace to include the "python" interpreter:然后我更改了工作区中的 launch.json 文件以包含“python”解释器:

{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "python": "c:/Program Files (x86)/Python37-32/python.exe"
        }
    ]
}

However, when I create the virtual environment:但是,当我创建虚拟环境时:

py -3 -m venv 32_bit_env

The python interpreter it uses is the 64-bit version from C:\Program Files\Python37\python.exe as shown in the pyvenv.cfg:它使用的 python 解释器是来自 C:\Program Files\Python37\python.exe 的 64 位版本,如 pyvenv.cfg 所示:

home = C:\Program Files\Python37

Is there another location to change the directory for the python.exe file in the workspace?是否有其他位置可以更改工作区中 python.exe 文件的目录? Thank you!谢谢!

There are two concepts you are mixing: The Python Interpreter used by VS Code and the py launcher.您正在混合两个概念:VS Code 和py启动器使用的 Python 解释器。 In addition to that, there is python command.除此之外,还有python命令。 I will try to explain how they are configured and how they affect and what.我将尝试解释它们是如何配置的以及它们是如何影响的以及什么。

1. python command 1. python命令

1.1. 1.1。 Used version specified by使用的版本指定

PATH environment variable only.PATH环境变量。 The first folder in the Process PATH environment variable with python.exe is used.使用带有python.exe的 Process PATH环境变量中的第一个文件夹。 Process PATH is formed by combining User and System PATH variables when starting the process.进程PATH是在启动进程时通过组合用户和系统 PATH 变量形成的。 Subprocesses, such as the integrated terminal of VS Code inherit the PATH of the parent process during initialization.子进程,例如 VS Code 的集成终端,在初始化时继承父进程的PATH

  • If virtual environment is active, the used version is the one pointed by the home key of the pyvenv.cfg .如果虚拟环境处于活动状态,则使用的版本是pyvenv.cfghome键指向的版本。 Activating virtual environment modifies the PATH of the shell.激活虚拟环境会修改 shell 的PATH

1.2. 1.2. Used in用于

When you run python command in a terminal.当您在终端中运行python命令时。

2. py command py命令

The py command is the Python Launcher for Windows , added in Python 3.3. py命令是 Windows 的Python 启动器,在 Python 3.3 中添加。

2.2 Used version Specified by 2.2 使用的版本

First, these are checked in this order首先,这些是按这个顺序检查的

  • An active virtual environment活跃的虚拟环境
  • A shebang line in the script (if present)脚本中的 shebang 行(如果存在)
  • With -2 or -3 flag a matching PY_PYTHON2 or PY_PYTHON3 Enviroment variable使用 -2 或 -3 标志匹配 PY_PYTHON2 或 PY_PYTHON3 环境变量
  • A PY_PYTHON Enviroment variable一个 PY_PYTHON 环境变量
  • From [defaults] in py.ini in your %LOCALAPPDATA%\py.ini来自 %LOCALAPPDATA%\py.ini 中 py.ini 中的 [defaults]
  • From [defaults] in py.ini beside py.exe (use where py to locate)来自 py.exe 旁边的 py.ini 中的 [defaults](使用where py定位)

It will always use the latest installed version, if not specified otherwise.如果未另行指定,它将始终使用最新安装的版本。

3. VS Code python.pythonPath 3.VS代码python.pythonPath

3.1. 3.1。 Used version specified by使用的版本指定

In the .vscode\settings.json , the python.pythonPath key..vscode\settings.json中, python.pythonPath键。

3.2. 3.2. Used in用于

  • When you use Python: Run Python File in Terminal in VS Code当您使用Python: Run Python File in Terminal
  • When you use Run: Run Without Debugging for.py file in VS Code当你在 VS Code 中使用Run: Run Without Debugging for.py 文件时
  • When you use Run: Start Debugging for.py file in VS Code使用Run: Start Debugging for.py 文件
  • When you launch a new integrated terminal in VS Code, if the python.exe is inside a virtual environment , it will be activated.在 VS Code 中启动新的集成终端时,如果 python.exe 在虚拟环境中,它将被激活。
  • Language services, like auto-complete, linting, syntax-checking, formatting.语言服务,例如自动完成、linting、语法检查、格式化。 Only those that are not ran using the integrated terminal.只有那些没有使用集成终端运行的。

Specifically, the python.pythonPath setting does not affect to what happens, if you run py or python in the integrated terminal.具体来说,如果您在集成终端中运行pypython ,则python.pythonPath设置不会影响发生的情况。 It can only affect it if you have specified a virtual environment for python.pythonPath , and launched new integrated terminal, and the virtual environment is activated.只有为python.pythonPath指定了虚拟环境,并启动了新的集成终端,并激活了虚拟环境,才会影响它。 That is, there is no extra "magic" added VS Code or the VS Code integrated terminal.也就是说,没有额外的“魔法”添加 VS Code 或 VS Code 集成终端。

The integrated terminal is (by default) just a regular Powershell.集成终端(默认情况下)只是一个普通的 Powershell。 It has no clue about your python.pythonPath settings in your VS Code.它对 VS 代码中的python.pythonPath设置一无所知。

How to force using the 32-bit Python 3.7?如何强制使用 32 位 Python 3.7?

You can create a virtual environent with python 3.7-32bit using您可以使用 python 3.7-32bit 创建一个虚拟环境

py -3.7-32 -m venv 32_bit_env

Or, the full filepath to the 32-bit v.3.7.或者,32 位 v.3.7 的完整文件路径。 python.exe , assuming Powershell (hence & ): python.exe ,假设 Powershell (因此& ):

& "C:\some\path\Python 3.7 32-bit\python.exe" -m venv 32_bit_env

You can check the full filepath with py -0p , if you need to.如果需要,您可以使用py -0p检查完整的文件路径。 Then, you can then make this virtual environment to be automatically activated in new VS Code integrated terminals, by editing the settings.json:然后,您可以通过编辑 settings.json 使这个虚拟环境在新的 VS Code 集成终端中自动激活:

{
    "python.pythonPath": "C:/tmp/someproj/my_venv/Scripts/python.exe"
}

Note that every \ in the entry has to be replaced with either \\ or / .请注意,条目中的每个\都必须替换为\\/ The python.pythonPath has to be absolute. python.pythonPath必须是绝对的。

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

相关问题 如何为Python指定虚拟环境 - How can I specify virtual environment for Python 如何指定python的确切版本作为虚拟环境要求的一部分 - How to specify exact version of python as part of virtual environment requirements 如何指定用于创建虚拟环境的 python 版本? - How to specify python version used to create Virtual Environment? 如何使用 VSCode 在虚拟环境中调试 python 代码? - How can I debug a python code in a virtual environment using VSCode? 为什么我在创建新的 conda 环境时必须指定 python 版本? - Why do I have to specify the python version when creating a new conda environment? 如果我有多个Python版本,如何将第三方库安装到特定的Python版本中? - How can I install third party libraries into the spcified Python version if I have multiple Python versions? 用virtualenvwrapper新建虚拟环境时如何选择Python版本? - How to choose Python version when making a new virtual environment with virtualenvwrapper? VScode Intellisense无法识别点子安装的软件包(Python) - VScode intellisense not recognizing pip intalled packages (python) 当我已经安装了python 3.6时,如何在运行python(和pipenv)2.7的pycharm中设置虚拟环境? - How do I set up a virtual environment in pycharm which runs python (and pipenv) 2.7 when I already have python 3.6 installed? 虚拟环境错误的python版本 - Virtual environment wrong python version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM