英文:
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_A
和project_B
以工作区的形式打开,然后为每个工作区选择解释器,VSCode会记住你的选择,并在下次打开时仍然使用先前选择的解释器。
另一种方法是使用多根工作区
-
打开一个新窗口,使用
Add Folder to Workspace...
将两个文件夹添加到当前工作区, -
然后分别为这两个文件夹创建虚拟环境,使用<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> -->
Python: Create Environment...
,并选择相应文件夹的解释器。您还可以选择相应文件夹中的
.py
文件,然后点击右下角的Python版本进行切换
这些文档可能也会有用:
-
https://code.visualstudio.com/docs/python/python-tutorial#_select-a-python-interpreter
-
https://code.visualstudio.com/docs/python/environments#_working-with-python-interpreters
英文:
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, -
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.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.
{
"folders": [
{
"path": "project1",
"settings": {
"python.defaultInterpreterPath": "path_to_project1_virtualenv/bin/python"
}
},
{
"path": "project2",
"settings": {
"python.defaultInterpreterPath": "path_to_project2_virtualenv/bin/python"
}
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论