[英]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.我将尝试解释它们是如何配置的以及它们是如何影响的以及什么。
python
command python
命令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
。
home
key of the pyvenv.cfg
.pyvenv.cfg
的home
键指向的版本。 Activating virtual environment modifies the PATH
of the shell.PATH
。When you run python
command in a terminal.当您在终端中运行
python
命令时。
py
command py
命令The py
command is the Python Launcher for Windows , added in Python 3.3. py
命令是 Windows 的Python 启动器,在 Python 3.3 中添加。
First, these are checked in this order首先,这些是按这个顺序检查的
where py
to locate)where py
定位) It will always use the latest installed version, if not specified otherwise.如果未另行指定,它将始终使用最新安装的版本。
python.pythonPath
python.pythonPath
In the .vscode\settings.json
, the python.pythonPath
key.在
.vscode\settings.json
中, python.pythonPath
键。
Python: Run Python File in Terminal
in VS CodePython: Run Python File in Terminal
Run: Run Without Debugging
for.py file in VS CodeRun: Run Without Debugging
for.py 文件时Run: Start Debugging
for.py file in VS CodeRun: Start Debugging
for.py 文件 Specifically, the python.pythonPath
setting does not affect to what happens, if you run py
or python
in the integrated terminal.具体来说,如果您在集成终端中运行
py
或python
,则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
设置一无所知。
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.