136

I need help. VSCode will NEVER find poetry virtualenv interpreter no matter what I try.

Installed poetry Python package manager using a standard $ curl method as explained in the official documentation.

Started a project by $ poetry new finance-essentials_37-64, installed poetry environment with $ poetry install.

So now I can see that I indeed have a virtual environment by:

Jaepil@Jaepil-PC MINGW64 /e/VSCodeProjects/finance_essentials_37-64 $ poetry env list 
>> finance-essentials-37-64-SCQrHB_N-py3.7 (Activated)

and this virtualenv is installed at: C:\Users\Jaepil\AppData\Local\pypoetry\Cache\virtualenvs, which has finance-essentials-37-64-SCQrHB_N-py3.7 directory.

However, VSCode is unable to find this virtualenv in its 'select interpreter' command. I only see a bunch of Anaconda and Pipenv environments but not the poetry environment's interpreter that I've just made.

I also added "python.venvPath": "~/.cache/pypoetry/virtualenvs", to my settings.json as suggested in here, but to no avail. Still doesn't work.

I also tried an absolute path, by adding "python.venvPath": "C:\\Users\\Jaepil\\AppData\\Local\\pypoetry\\Cache\\virtualenvs", to the same settings, but it also doesn't work.

VSCode settings reference states that it has python.poetryPath as a default but it doesn't seem to work either. Should I change the default value "poetry" in this case?

python.poetryPath

"poetry"

Specifies the location of the Poetry dependency manager executable, if installed. The default value "poetry" assumes the executable is in the current path. The Python extension uses this setting to install packages when Poetry is available and there's a poetry.lock file in the workspace folder.

I'm on Windows 10 pro 64bit & Has Python 3.7.6 installed on the system.

PS C:\Users\Jaepil> python
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
user8491363
  • 2,924
  • 5
  • 19
  • 28

4 Answers4

348

You just need to type in your shell:

poetry config virtualenvs.in-project true

The virtualenv will be created inside the project path and vscode will recognize.


If you already have created your project, you need to re-create the virtualenv to make it appear in the correct place:

poetry env list  # shows the name of the current environment
poetry env remove <current environment>
poetry install  # will create a new environment using your updated configuration
Christian H
  • 3,779
  • 2
  • 7
  • 17
  • 20
    Actually this is quite good I like to keep my venv in the same place as project – mrWiecek Nov 02 '20 at 18:39
  • yes I also prefer keeping my venv inside the project root @mrWiecek – Christian H Nov 19 '20 at 03:26
  • Unfortunately, when using the fish shell, `poetry shell` breaks when there are spaces in the complete path name. If you are working in a directory with *any* spaces in any *any* ancestor director, then poetry will fail to work. – BallpointBen Feb 17 '21 at 15:51
  • @BallpointBen I have a poetry project located in C:\Programming\project with no spaces, but my user account directory (including the cache directory where the virtualenvs are normally stored) does have a space in the name. Also, after I ran this command, it did not move anything around. Do I need to do this from a scratch project? – Goodies Feb 18 '21 at 05:10
  • 1
    @Goodies yes, this only affects future environments, not existing ones. I’m not sure whether spaces are permissible for you; my only experience was with fish shell on a Mac. – BallpointBen Feb 18 '21 at 05:17
  • 4
    Understood. I was able to run `poetry env remove [existing environmet]` and then when I built it the next time, it built the new env in the correct location. Thank you! – Goodies Feb 18 '21 at 06:17
  • this doesn't work out-of-the-box if the project root contains multiple 'sub-projects'. In machine learning, it is common to have ```training``` and ```serving``` sub-directories. I have made this technique work by adding ```./training/.venv/lib/python3.7/site-packages``` (same for serving) into ```python.analysis.extraPaths``` in VSCode ```settings.json```. Is there a better way? – pink spikyhairman Jun 17 '21 at 13:17
  • If you allow spaces in your file path, you deserve the pain you experience. UNIX 101. – P i Jun 22 '21 at 17:25
  • When you do this, don't forget to add `${wordspaceFolder}` to vscode's Python: Venv Folders setting. Open settings (ctrl + , ) and type 'venv' to find this option. Click "Add Item" and type `${wordspaceFolder}` – user8491363 Jul 29 '21 at 14:22
  • Wanted to add that I had to reload the vscode window then select interpreter after doing this. Otherwise, vscode would not find python in `.venv` for some reason. – Joe Sadoski Jul 31 '21 at 23:03
  • @JoeSadoski what OS? – Christian H Oct 05 '21 at 01:00
  • @ChristianH This was on macOS 11.6 – Joe Sadoski Oct 06 '21 at 02:06
  • Strange, usually only reloading does it. – Christian H Oct 06 '21 at 06:46
  • I applied this answer to an existing project and ran into `CalledProcessError` and `EnvCommandError`. I solved the errors by copying `pyvenv.cfg` from the directory where the existing environment was located into the root directory of my project. Then I ran `poetry install` and it worked. – Banty May 12 '23 at 10:27
57

You need to set "python.venvPath": "C:\\Users\\Jaepil\\AppData\\Local\\pypoetry\\Cache\\virtualenvs" in your settings (the one you tried is for UNIX).

You can also https://github.com/microsoft/vscode-python/issues/8372 to help prioritize adding Poetry virtual environment support to the Python extension.

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

The settings have changed for the Python extension in VS Code. I was able to select my Poetry virtual environment for my interpreter/ipynb kernel again after changing the dated python.pythonPath setting (yours might be python.venvPath) to python.defaultInterpreterPath in the VS Code settings.json file.

Note: My work computer is a Mac but I expect this should work for Windows. To find the ~path, enter poetry env info --path in your CLI under the appropriate project folder, then tack on the subdirectory info where Python is installed. On MacOS/Linux, this ends with "/bin/python"; on Windows, "python.exe". https://code.visualstudio.com/docs/python/environments#_manually-specify-an-interpreter for more info.

{
    "python.defaultInterpreterPath": "/Users/myname/Library/Caches/pypoetry/virtualenvs/projectname-randomnumbers-py3.9/bin/python",
}
BLT
  • 415
  • 3
  • 9
8

You can add your virtualenvs folder to "python.venvFolders" in vs code global settings.

Like that for Windows:

"python.venvFolders": [
    "C:\\Users\\User\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\"
  ]
vodkar
  • 81
  • 2
  • 2