Search code examples
pythonpython-poetry

Packages installed with Poetry fail to import


Having a simple yet confusing issue: a package I added with poetry fails to import when I try to use it in a module. Steps taken:

  1. poetry add sendgrid
  2. In a module, import sendgrid
  3. Error: Import "sendgrid" could not be resolved PylancereportMissingImports

Troubleshooting I've tried:

  • I checked my project's poetry venv dir, and sendgrid is there: 'C:\\Users\\xyz123\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\nameofproject-py3.10\\lib\\site-packages'
  • Also checked sys.path(); the path to that site-packages dir is listed
  • Running poetry install gives me the response No dependencies to install or update
  • both the pyproject.toml and the poetry.lock files list sendgrid

What is going on?


Solution

  • Well, it turns out it's a matter of VSCode not playing nice and failing to recognize Poetry's virtual environment. I had to run the Python: Select Interpreter command and change the venv directory to the one my project is using, then it was able to recognize the installed packages.

    See here for more details on how to do that.