Selecting Python interpreter from WSL

Viewed 11077

I am using Windows 10 and want to set the the default VSCode interpreter for Python to be the same one used in my WSL 2 (Ubuntu), so that I'm always using "one Python".

When I click "Select Python interpreter" a prompt appears to find the interpreter path, but I'm not sure what the path would be.

3 Answers

I think that, in order to use the WSL python, you need to be running VSCode in a WSL remote window. You need to install the Remote WSL extension first. Then, on the lower left there is a green button that will let you start a WSL window, or it will tell you that you are already in one.

This shows that you are in a WSL window

If you are in a WSL window, you should be able to select your python interpreter pretty easily. It will either automatically detect it, or you can run which python3 in an Ubuntu terminal to get the path. If you want to navigate to a file on your Windows file system from within this window, look in, e.g., /mnt/c/Users/<USER_NAME>.

Well probably late to the party but you can find Python you are using on your WSL2 with simple command:

which python3

It will show you where is the python placed.

While above answers work, there are some related things to keep in mind here (or are at least worth mentioning):

  1. I would recommend to create a virtual environment for your project. If you do so, its very easy to find the python interpreter in the bin folder of the venv. (If you want to "find" your WSL folder in windows. Just enter explorer.exe . in your terminal - it will open a windows explorer in the current location.
  2. If you open the python interpreter selector form within VS Code on Windows, be aware that it opens a windows that let's you select "Executables" (meaning WINDOWS wxecutables): enter image description here

You will not be able to open your python interpreter located in your wsl like that, as the folder will be shown as empty.

Related