Auto-switching Python Virtual Environments in Visual Studio Code per Directory within a Workspace.

huangapple go评论71阅读模式
英文:

Auto-switching Python Virtual Environments in Visual Studio Code per Directory within a Workspace

问题

I am working on a project in VSCode that has multiple directories, each of which requires a different Python virtual environment. My virtual environments are located in the ~/.virtualenvs directory and my workspace is structured like this:

~/.virtualenvs/
├── venv_A/
└── venv_B/

my_workspace/
├── project_A/
│   └── script_A.py
└── project_B/
    └── script_B.py

I want VSCode to automatically switch to the appropriate virtual environment (venv_A for project_A, and venv_B for project_B) located in ~/.virtualenvs when I open a Python file from each directory within the workspace. Currently, I have to manually select the virtual environment through the command palette each time.

I have tried looking through the VSCode documentation and searched for guides or tutorials on how to achieve this functionality, but I haven't found anything that addresses this specific issue. I expected there to be some configuration options either through the .vscode/settings.json file or the workspace settings that would allow me to specify which virtual environment should be used for each directory, and how Pylance, pylint, and yapf should adapt accordingly.

I am aware that VSCode has support for workspaces and .env files, but I'm not sure how to configure it to auto-switch virtual environments based on the directory, and to have Pylance, pylint, and yapf adapt accordingly.

I also found the issue Select pyenv environment based on folder .python-version file that is not closed.

英文:

I am working on a project in VSCode that has multiple directories, each of which requires a different Python virtual environment. My virtual environments are located in the ~/.virtualenvs directory and my workspace is structured like this:

~/.virtualenvs/
│
├── venv_A/
│
└── venv_B/

my_workspace/
│
├── project_A/
│   └── script_A.py
│
└── project_B/
    └── script_B.py

I want VSCode to automatically switch to the appropriate virtual environment (venv_A for project_A, and venv_B for project_B) located in ~/.virtualenvs when I open a Python file from each directory within the workspace.
Currently, I have to manually select the virtual environment through the command palette each time.

I have tried looking through the VSCode documentation and searched for guides or tutorials on how to achieve this functionality, but I haven't found anything that addresses this specific issue.
I expected there to be some configuration options either through the .vscode/settings.json file or the workspace settings that would allow me to specify which virtual environment should be used for each directory, and how Pylance, pylint, and yapf should adapt accordingly.

I am aware that VSCode has support for workspaces and .env files, but I'm not sure how to configure it to auto-switch virtual environments based on the directory, and to have Pylance, pylint, and yapf adapt accordingly.

I also found the issue Select pyenv environment based on folder .python-version file that is not closed.

答案1

得分: 1

最简单的方法是分别将project_Aproject_B以工作区的形式打开,然后为每个工作区选择解释器,VSCode会记住你的选择,并在下次打开时仍然使用先前选择的解释器。

另一种方法是使用多根工作区

  • 打开一个新窗口,使用Add Folder to Workspace...将两个文件夹添加到当前工作区,

    Auto-switching Python Virtual Environments in Visual Studio Code per Directory within a Workspace.
    Auto-switching Python Virtual Environments in Visual Studio Code per Directory within a Workspace.

  • 然后分别为这两个文件夹创建虚拟环境,使用<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> --> Python: Create Environment...,并选择相应文件夹的解释器。

    Auto-switching Python Virtual Environments in Visual Studio Code per Directory within a Workspace.

    您还可以选择相应文件夹中的.py文件,然后点击右下角的Python版本进行切换

这些文档可能也会有用:

英文:

The easiest way is to open project_A and project_B as workspaces respectively, and then select an interpreter for the workspace, vscode will remember your choice, and will still use the previously selected interpreter when it is opened next time.

Another approach is to use Multi-root Workspaces

  • Open a new window and use Add Folder to Worspace... to add both folders to the current workspace,

    Auto-switching Python Virtual Environments in Visual Studio Code per Directory within a Workspace.
    Auto-switching Python Virtual Environments in Visual Studio Code per Directory within a Workspace.

  • Then created separate virtual environments for both folders using <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> --> Python: Create Environment... and select the interpreter for the respective folder.

    Auto-switching Python Virtual Environments in Visual Studio Code per Directory within a Workspace.

    You can also select the .py file in the corresponding folder, and then click the python version in the lower right corner to switch

These documents may also be useful:

答案2

得分: 0

我尚未尝试过这种方法,但似乎你可以在工作空间配置文件中为每个项目定义虚拟环境。

{
	"folders": [
		{
			"path": "project1",
			"settings": {
				"python.defaultInterpreterPath": "path_to_project1_virtualenv/bin/python"
			}
		},
		{
			"path": "project2",
			"settings": {
				"python.defaultInterpreterPath": "path_to_project2_virtualenv/bin/python"
			}
		}
	]
}
英文:

I haven't tried this, but it appears you can define each project's virtual environment in the Workspace configuration file.

{
	&quot;folders&quot;: [
		{
			&quot;path&quot;: &quot;project1&quot;,
			&quot;settings&quot;: {
				&quot;python.defaultInterpreterPath&quot;: &quot;path_to_project1_virtualenv/bin/python&quot;
			}
		},
		{
			&quot;path&quot;: &quot;project2&quot;,
			&quot;settings&quot;: {
				&quot;python.defaultInterpreterPath&quot;: &quot;path_to_project2_virtualenv/bin/python&quot;
			}
		}
	]
}

huangapple
  • 本文由 发表于 2023年6月8日 21:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定