How to set the Ubuntu Python3 interpreter path in VS Code?

Question:

I’m using Python3 on Ubuntu 20.04 inside WSL2, running print(sys.executable) outputs /usr/bin/python3

Within VS Code, pressing "Select Interpreter" then "Enter Interpreter Path" I have tried both /usr/bin/python3 and the full windows path \wsl$Ubuntu/usrbin/python3 (which is reachable inside file explorer).

Both paths returns the error An Invalid Python interpreter is selected, please try changing it to enable features such as IntelliSense, linting, and debugging.

It seems to only accept an .exe file, how can I select the Linux path as the interpreter? Thanks.

Asked By: Parfitarole

||

Answers:

It sounds like either:

  • You are starting VSCode from Windows, rather than from inside WSL
  • Or you don’t have the WSL extension installed
  • Or both

There are several options for doing WSL development in VSCode, but all do require that you start by installing (if you haven’t already) either:

  • The "Remote – WSL" extension
  • Or the "Remote Development" extension pack, which includes the former

Then, typically, you’ll start VSCode by navigating to your project directory in WSL and:

code .

You’ll see that VSCode has switched from "Windows" mode to "WSL" mode in the lower left-hand corner of the status-bar, where you should see "WSL: [distroname]".

With that in place, you should be able to select the Linux Python interpreter.

Alternatively, you can start VSCode through Windows, then switch to WSL mode with the stacked "><" icon in that same location on the status-bar. Or just enter the command-palette and "Remote-WSL: New WSL Window".

Answered By: NotTheDr01ds