Use Poetry for Python dependency management with Visual Studio Code

Python has been the most popular programming language for years, but despite this dependency management has been a weak spot for a long time as well. Pipenv was (and is) a good attempt to fix this, but in late 2019 Poetry 1.0 was released and it is very good. Seriously, give it a try.

There is a small problem though: Poetry uses venv for installing dependencies: when you add dependencies to a Poetry Python project, a new virtual environment is created where the dependencies are installed. However, Visual Studio Code doesn't automatically detect venv virtual environments which results in linting errors where the editor complains that dependencies cannot be resolved.

There is a fix though: in your settings.json you can specify the path where your virtual environments are stored. You can specify it like this:

    "python.venvPath": "~/.cache/pypoetry/virtualenvs"

You can also use the GUI to edit this setting:

Now you can select the correct Python interpreter that is located in your virtual environment for your Poetry project. You can open the dialog to do this in two ways:

  1. On the bottom left corner, click on the Python version used
  2. CTRL + Shift + P and search for "Python: select interpreter"

In this dialog the virtual environments stored in the path you specified in the settings should now appear.

Show Comments