My post on configuring Pipenv to work with Visual Studio Code is the most visited post on my blog, and seems to have helped a lot of people. This weekend I tried Poetry as an alternative to Pipenv, so here's how you set it up in VS Code.

After setting up a Poetry project with poetry init, and adding your dependencies with poetry add django etc, a virtualenv will be created.

You need to find the full path to your project's virtualenv. This can be done by running:

$ poetry shell

This is similar to what you would do with Pipenv, so from now on, running python somefile.py will use the Python interpreter for this Poetry project.

After running the command above, you should see this printed:

Spawning shell within /home/myuser/.cache/pypoetry/virtualenvs/myproject-py3.7

Copy the full path (/home/myuser/.cache/pypoetry/virtualenvs/myproject-py3.7) and open your project in VS Code. Now create a folder called .vscode in your project's root folder if it's not already there. Inside it, create a file called settings.json, and enter the following:

{
    "python.pythonPath": "/home/myuser/.cache/pypoetry/virtualenvs/myproject-py3.7"
}

Visual Studio Code (and the Python Code Server plugin) should now be able to analyze your project and dependencies.