Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode doesn't show poetry virtualenvs in select interpreter option

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 
like image 842
user8491363 Avatar asked Jan 23 '20 16:01

user8491363


People also ask

How do you select the correct interpreter in Visual Studio code?

To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).

How do I create a poetry virtual environment in Visual Studio Code?

First, open the project in VS Code. Then, via the command palette ( CTRL+SHIFT+P ), open the Workspace Settings (JSON). Make sure you edit the Workspace settings.json and not your global settings.json! You want to edit the workspace settings to use this poetry virtual environment and not all of your python project to use this particular virtualenv.

How to select the correct Python interpreter for my Poetry Project?

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: On the bottom left corner, click on the Python version used CTRL + Shift + P and search for "Python: select interpreter"

Does Visual Studio Code automatically detect venv virtual environments?

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.

Is it possible to use venv with poetry?

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.


1 Answers

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 
like image 170
Christian H Avatar answered Oct 02 '22 12:10

Christian H