Can I have multiple subfolders with virtual python environments in VS Code?
Asked Answered
R

2

13

I have a monorepo structured like this:

myRepo/
├─ project_1/
│  ├─ .venv/
│  ├─ main.py
├─ project_2/
│  ├─ .venv/
│  ├─ main.py
├─ .gitignore
├─ README.md
  • Can VS Code handle multiple python venvs in subfolders?

After some googling I managed a find one solution, but its not very elegant. I created a workspace and added the folders /project_1 and /project_2, that worked and I can easily switch and select Python Interpreter. I also need to modify files in /root from time to time so I added this folder as well. All this makes the Explorer folder structure bigger and more cluttered with duplicates of itself:

workspace/
├── myRepo/
│   ├── project_1/
│   │   ├── .venv/
│   │   └── main.py
│   ├── project_2/
│   │   ├── .venv/
│   │   └── main.py
│   ├── .gitignore
│   ├── README.md
│   └── myRepo/
├── project_1/
│   ├── .venv/
│   └── main.py
└── project_2/
    ├── .venv/
    └── main.py
Riggle answered 25/10, 2021 at 13:21 Comment(2)
What was the problem with the original file structure?Jeannajeanne
VS Code can't find the venvs it seems. #61148770Riggle
W
3

VS Code has a list of places, where it looks for virtual environments. Only environments located directly under the workspace are picked up automatically. You can also enter custom paths when running the Python: Select Interpreter command, though. Simply select "Enter interpreter path..." and navigate to your venv's /bin/python executable:

select interpreter

enter interpreter path

find

python binary

Once you have used a cutom interpreter path, it is known to VS code and will be directly selectable using the Python: Select Interpreter command.

Weaner answered 25/10, 2021 at 14:6 Comment(4)
If this works for you then there is definitely something wrong on my side. I've tried this several times, it just swaps back to the Interpreter I last used. Are you sure this works in a sub dir?Riggle
This works with any path in your workspace (and even out of your workspace). Did you press return on the "Find..." selection in the third screenshot? This should open a treeview in which you can navigate through your directories. Be sure to select the python binary, not the venv's path. I will update the answer with another screenshot.Weaner
Hmm, yes I used the Finder navigator didn't work - but it actually work when writing the path without the navigator like this ./project_1/.venv/bin/python I'm on macOS btw. Looks like its a bit buggy. Thanks!Riggle
@Riggle Your comment helped me get the base interpreter running on macOS. But I have yet to find out how to get it to work with interactive Jupyter notebooks.Chaechaeronea
I
2

Hard to figure out, took me a few hours so I'll leave this here for reference.

How to create separate virtual environments: microsoft/vscode-python "Mono Repo Set-up Guide"

TL;DR

  1. Open root folder (myRepo)
  2. Click on File > Add Folder to Workspace...
  3. Select all project folders you want to add as a workspace (project_1, project_2)
  4. For each project/workspace, create a virtual environment.
Inure answered 1/4 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.