当我安装了多个 python 版本时,如何为 VSCode 虚拟环境指定 python 版本?
How to specify the python version for VSCode virtual environment, when I have multiple python versions intalled?
我同时安装了 64 位和 32 位 Python。我试图使用 VSCode 中的 32 位 Python.exe 文件创建一个虚拟环境。我在 Python: Select Interpreter 中为我的工作区选择了那个版本(即 C:\Program Files (x86)\Python37-32\python.exe)。
然后我更改了工作区中的 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"
}
]
}
但是,当我创建虚拟环境时:
py -3 -m venv 32_bit_env
它使用的 python 解释器是来自 C:\Program Files\Python37\python.exe 的 64 位版本,如 pyvenv.cfg:
home = C:\Program Files\Python37
是否有另一个位置可以更改工作区中 python.exe 文件的目录?谢谢!
您混淆了两个概念:VS Code 使用的 Python 解释器和 py
启动器。除此之外,还有 python
命令。我将尝试解释它们是如何配置的以及它们如何影响以及什么。
1。 python
命令
1.1。
指定的使用版本
PATH
仅限环境变量。使用进程 PATH
环境变量中带有 python.exe
的第一个文件夹。进程 PATH
是在启动进程时通过组合用户和系统 PATH 变量形成的。子进程,比如VS Code的集成终端,初始化时继承父进程的PATH
- 如果虚拟环境是活动的,使用的版本是
pyvenv.cfg
的home
键指向的版本。激活虚拟环境修改 shell. 的 PATH
1.2。用于
当您在终端中 运行 python
命令时。
2。 py
命令
py
命令是Python Launcher for Windows,在Python3.3.
中添加
2.2 使用的版本由
指定
首先,这些按顺序检查
- 一个活跃的虚拟环境
- 脚本中的 shebang 行(如果存在)
- 带有 -2 或 -3 标志的匹配 PY_PYTHON2 或 PY_PYTHON3 环境变量
- 一个PY_PYTHON环境变量
- 从 [defaults] in py.ini in your %LOCALAPPDATA%\py.ini
- 来自[defaults] in py.ini beside py.exe(使用
where py
定位)
它将始终使用 最新 安装版本,如果没有另外指定。
3。 VS 代码 python.pythonPath
3.1。
指定的使用版本
在.vscode\settings.json
中,python.pythonPath
键。
3.2。用于
- 在 VS Code 中使用
Python: Run Python File in Terminal
时
- 在 VS Code 中对 .py 文件使用
Run: Run Without Debugging
时
- 当您在 VS Code 中对 .py 文件使用
Run: Start Debugging
时
- 当您在 VS Code 中启动新的集成终端时,如果 python.exe 在虚拟环境中,它将被激活。
- 语言服务,例如自动完成、linting、语法检查、格式化。仅那些未 运行 使用集成终端的用户。
具体来说,python.pythonPath
设置 不会 影响发生的情况,如果您 运行 py
或 python
综合终端。只有为 python.pythonPath
指定了虚拟环境,并启动新的集成终端,并激活了虚拟环境,它才会影响它。即没有额外“神奇”的添加VS Code或VS Code集成终端。
集成终端(默认情况下)只是一个普通电源shell。它不知道您在 VS Code 中的 python.pythonPath
设置。
如何强制使用32位Python 3.7?
您可以使用
创建一个 python 3.7-32 位的虚拟环境
py -3.7-32 -m venv 32_bit_env
或者,32 位 v.3.7 的完整文件路径。 python.exe
,假设幂shell(因此&
):
& "C:\some\path\Python 3.7 32-bit\python.exe" -m venv 32_bit_env
如果需要,您可以使用 py -0p
检查完整的文件路径。然后,您可以通过编辑 settings.json:
使这个虚拟环境在新的 VS Code 集成终端中自动激活
{
"python.pythonPath": "C:/tmp/someproj/my_venv/Scripts/python.exe"
}
请注意,条目中的每个 \
都必须替换为 \
或 /
。 python.pythonPath
必须是绝对的。
我同时安装了 64 位和 32 位 Python。我试图使用 VSCode 中的 32 位 Python.exe 文件创建一个虚拟环境。我在 Python: Select Interpreter 中为我的工作区选择了那个版本(即 C:\Program Files (x86)\Python37-32\python.exe)。
然后我更改了工作区中的 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"
}
]
}
但是,当我创建虚拟环境时:
py -3 -m venv 32_bit_env
它使用的 python 解释器是来自 C:\Program Files\Python37\python.exe 的 64 位版本,如 pyvenv.cfg:
home = C:\Program Files\Python37
是否有另一个位置可以更改工作区中 python.exe 文件的目录?谢谢!
您混淆了两个概念:VS Code 使用的 Python 解释器和 py
启动器。除此之外,还有 python
命令。我将尝试解释它们是如何配置的以及它们如何影响以及什么。
1。 python
命令
1.1。
指定的使用版本PATH
仅限环境变量。使用进程 PATH
环境变量中带有 python.exe
的第一个文件夹。进程 PATH
是在启动进程时通过组合用户和系统 PATH 变量形成的。子进程,比如VS Code的集成终端,初始化时继承父进程的PATH
- 如果虚拟环境是活动的,使用的版本是
pyvenv.cfg
的home
键指向的版本。激活虚拟环境修改 shell. 的
PATH
1.2。用于
当您在终端中 运行 python
命令时。
2。 py
命令
py
命令是Python Launcher for Windows,在Python3.3.
2.2 使用的版本由
指定首先,这些按顺序检查
- 一个活跃的虚拟环境
- 脚本中的 shebang 行(如果存在)
- 带有 -2 或 -3 标志的匹配 PY_PYTHON2 或 PY_PYTHON3 环境变量
- 一个PY_PYTHON环境变量
- 从 [defaults] in py.ini in your %LOCALAPPDATA%\py.ini
- 来自[defaults] in py.ini beside py.exe(使用
where py
定位)
它将始终使用 最新 安装版本,如果没有另外指定。
3。 VS 代码 python.pythonPath
3.1。
指定的使用版本在.vscode\settings.json
中,python.pythonPath
键。
3.2。用于
- 在 VS Code 中使用
Python: Run Python File in Terminal
时 - 在 VS Code 中对 .py 文件使用
Run: Run Without Debugging
时 - 当您在 VS Code 中对 .py 文件使用
Run: Start Debugging
时 - 当您在 VS Code 中启动新的集成终端时,如果 python.exe 在虚拟环境中,它将被激活。
- 语言服务,例如自动完成、linting、语法检查、格式化。仅那些未 运行 使用集成终端的用户。
具体来说,python.pythonPath
设置 不会 影响发生的情况,如果您 运行 py
或 python
综合终端。只有为 python.pythonPath
指定了虚拟环境,并启动新的集成终端,并激活了虚拟环境,它才会影响它。即没有额外“神奇”的添加VS Code或VS Code集成终端。
集成终端(默认情况下)只是一个普通电源shell。它不知道您在 VS Code 中的 python.pythonPath
设置。
如何强制使用32位Python 3.7?
您可以使用
创建一个 python 3.7-32 位的虚拟环境py -3.7-32 -m venv 32_bit_env
或者,32 位 v.3.7 的完整文件路径。 python.exe
,假设幂shell(因此&
):
& "C:\some\path\Python 3.7 32-bit\python.exe" -m venv 32_bit_env
如果需要,您可以使用 py -0p
检查完整的文件路径。然后,您可以通过编辑 settings.json:
{
"python.pythonPath": "C:/tmp/someproj/my_venv/Scripts/python.exe"
}
请注意,条目中的每个 \
都必须替换为 \
或 /
。 python.pythonPath
必须是绝对的。