3

VSCode supports many virtual environments as default and those environments' files are searchable. However, I'm using Poetry and its libraries don't seem to come up on the search.

I manually set my Python interpreter by changing .vscode/settings.json in my project directory. (Because command palette's Python: Select interpreter didn't work either.

{
    "python.pythonPath": "~\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\finance-essentials-37-64-58e2e1Bc-py3.7\\Scripts"
}

I want to make files in ~\AppData\Local\pypoetry\Cache\virtualenvs\finance-essentials-37-64-58e2e1Bc-py3.7\Lib\site-packages searchable on my command pelette & code context so that I can look up library sources easily.

How do I do this?

user8491363
  • 2,924
  • 5
  • 19
  • 28
  • You might want to ask on Poetry's repo too. https://github.com/python-poetry/poetry/issues – AKX Feb 03 '20 at 10:28
  • 1
    @AKX Right, but Poetry issue rules state that I should ask tips & tricks on SO. "For support questions, please post on StackOverflow." – user8491363 Feb 03 '20 at 10:31
  • Answered on https://stackoverflow.com/questions/59882884/vscode-doesnt-show-poetry-virtualenvs-in-select-interpreter-option/64434542#64434542 – Christian H Oct 22 '20 at 16:36

2 Answers2

5

I like this question, actually. Being able to search and look through packages easily makes one more productive.

There's a good answer here.

VS Code multi-root workspaces explained in-depth here, as fellow snarky Canadian @BrettCannon mentioned.

In VS Code, you just need to click on File -> Add Folder to Workspace... and locate your Poetry virtual environment, or wherever your site-packages folder is, which contains your externally-installed libraries/packages.

Or... to do the exact-same-thing as above the hard way... Just create a JSON-based workspace.code-workspace file in your .vscode directory, alongside your launch.json and settings.json files. This will load your multi-root workspace automatically when you reload VS Code. Then paste in the following contents, changing the second path as required for your own site-packages folder:

{
    "folders": [
        {
            "path": ".."
        },
        {
            "path": "../../usr/local/lib/python3.9/site-packages"
        }
    ],
    "settings": {}
}
Sean McCarthy
  • 4,838
  • 8
  • 39
  • 61
  • is there a way to use env variable in workspace definition? like `"path": "$VIRTUAL_ENV"`? – ClementWalter Jun 15 '22 at 10:00
  • 1
    actually here https://code.visualstudio.com/docs/editor/variables-reference#_environment-variables; I understood that "/${env:VIRTUAL_ENV}" should work but it doesn't. when I "copy path", it returns this exact string without interpolation – ClementWalter Jun 15 '22 at 15:29
-1

You could use a multi-root workspace that includes your code and the site-packages directory.

Although I would ask why you feel the need to have installed 3rd-party code searchable in that way? General code navigation from within the Python extension should help you discover details of the code you're using and documentation should be the primary way to learn about the code you're using.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40