How to activate virtual environments in VS Code using Poetry and WSL

For my newest project, I decided to switch from Pipenv to Poetry. I initialised my project with poetry init and added all dependencies. After running poetry shell, a confirmation appeared in the terminal that the virtual environment was activated. But I couldn’t see the environment in the VS Code status bar. And when trying to select the Python interpreter via the command palette, the environment wasn’t listed either.

By default, Poetry creates virtual environments in {cache-dir}/virtualenvs. In my case using WSL, this is ~/.cache/pypoetry/virtualenvs. So I tried to enter this path in the VS Code command palette. But this led to an error message.

Poetry allows the use of settings that are specific for a project and stored in the poetry.toml file. For virtual environments, it can be specified that they should be created inside the project’s root directory. So I added

Create = true
In-project = true

to this file. (This could also have been done in the terminal, see summary below.) And I deleted the virtual environment in the above mentioned virtualenvs directory. I also manually created the .venv directory. But now, running the command poetry install caused an error message that python couldn’t be found. Restarting the terminal and VS Code had no effect.

To solve this situation, I had to install the package python-is-python3 in WSL. Now, after running the command poetry install, the virtual environment was created inside the .venv directory. Also, VS Code noted the virtual environment and asked whether it should be activated for the workspace, and it now is listed among the interpreters in the command palette.

To summarize, follow these steps to activate virtual environments in VS Code using Poetry and WSL:

  • In WSL, install the package python-is-python3

  • In your shell, either run the command poetry config true

OR create a poetry.toml file in your project’s root directory and add

Create = true
In-project = true
  • (If not already done, run poetry init to create the pyproject.toml file)

  • Run poetry install, to resolve the dependencies form the pyproject.toml file, and install them

The virtual environment will now be created and activated!

WslPoetryVirtual EnvironmentsVs Code
Avatar for Scott Böning

Written by Scott Böning

Software engineer using mostly Python with a passion for coffee and cats.


Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.