DEV Community


Relative Path to Virtual Env with VS Code

Kevin Hulquest
・2 min read

Given there is nothing to do outside the house these days, I thought I would fill some time this weekend and hack on some python code. The code I wanted to work on was a small side project located in a directory that was not the project root. Visual Studio Code calls this the ${workspaceDir}.

The utility I wrote had a small set of dependencies and I organized them into a virtual environment. A virtual environment in the directory of the code I wrote; so again it's off the top level project directory. The problem I ran into was that Code didn't recognize this virtual environment since (I believe) it is not directly in the workspace directory. alt text

This isn't an insurmountable problem for sure but finding the answer proved to be more difficult than I thought. So I am writing an article to unify the information I found in separate corners of the internet.

The image above is produced when you search for the "Python Interpreter" command. To add the virtualenv in a subdirectory, I had to edit the workspace settings. You can do this by typing F1 followed by "workspace setting". This will open properties dialog. alt-text

There are many properties to chose from and this is where I ran into trouble. Exactly which setting will allow me to configure a relative python virtualenv? Searching for python.pythonPath is the silver bullet in this case. Once found, you can enter the full or relative path to your python virtual environment. alt-text You'll notice I annotated the picture with red and green arrows. The red arrow indicates the top level project directory while the green is the subdirectory that contains the python virtualenv directory called venv.

Discussion (0)

Forem Open with the Forem app