Selecting Python interpreter from WSL - visual-studio-code

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.

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.
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):
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.
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):
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

why my vscode does not have the same libraries installed in wsl?

I'm using wsl and it runs codes in vscode pretty fine and I have different libraries which I installed through pip and conda in wsl but when I run that code using vscode itself it doesn't recognize the libraries or even pip itself.
I don't have any other environment.
I should add that I installed the packages globally using conda install ... or pip install ... in base environment and I only have base environment and I run my code through code . and I also have python and remote wsl extensions installed in my vscode.
what can be the problem?
I don't have much personal experience with this, but I found some useful information in this Stack Overflow question (even though it doesn't utilize conda), along with https://code.visualstudio.com/docs/remote/wsl-tutorial#_python-development.
I also found this blog post useful, even if it doesn't cover WSL.
In short, make sure you:
Have installed the Python extension (by Microsoft) in VSCode. This is critical for being able to detect and select the Python interpreter. You don't mention having this in place, so I believe this is your likely problem.
You have done this already, but including it for others who might read this later -- Install the Remote - WSL extension (or the Remote Development extension pack) in VSCode.
You are also doing this already -- Start VSCode from inside your WSL distribution. Alternatively, you can start VSCode from Windows and then select the Remote WSL - Reopen Folder in WSL from the Command Palette (also accessible from the "Remote" Status Bar).
In VSCode, open the Command Palette with Shift+Ctrl+P, search for the Python: Select Interpreter command, and you should find your Conda environment in the list.
After selecting this, you should find that your project is using the interpreter and modules that you have installed via conda.
One thing I did to overcome this issue is go to Extensions -> Local (You should have two tabs there, Local and WSL:DISTRO) DISTRO refers to whatever DISTRO you're using, you will see that some of the local extensions are disabled in the current workspace (WSL) and there is a little cloud icon in the WSL:DISTRO tab that says install Local Extensions in WSL:DISTRO once you click that it will let you choose which extensions to install and you should be good to go!

Intellisense autocomplete not working for wsl in vscode

I've been setting up vscode as an IDE for python development. I use windows subsystem for linux with my python packages installed through anaconda there.
My problem is that auto completion does not work as expected. For examples, outside of wsl if I am using the numpy package I can get autocompletion:
Autocomplete outside of wsl
However, inside of wsl autocomplete does not find numpy.linalg (as in example above):
Autocomplete inside wsl
Thanks in advance!
edit: I am currently using wsl1 if that information is relavent!
As mentioned by #Adriana Hernández you need to install the extensions inside WSL too.
The VSCode WSL docs are covering these here
In short if you go to the extensions tab, you shall see Python marked with "Install in WSL...". Once clicked install and reloaded, it should work.
If not you likely need to pick another interpreter (likely the one from your venv.)
hit Ctrl+Shift+P then
Python: Select interpreter
then select the desired python interpreter (most likely the one in your venv which has all the packages)
What works for me is creating a venv, activating then launching VSCode from WSL with code .
On the first time you will need to install your extensions to WSL, but then it should be good to go.

How to use Python3 on the VScode terminal?

Is there a way to force VS Code to use only python3? It always defaults to python2.7 no matter what I try. I've tried selecting the correct interpreter as python3.7. When I open up terminal, it immediately uses python2.7, In the settings it is pointing at 3.7, but the built in terminal which is nice, always defaults to 2.7.
First, understand that the integrated terminal of VSCode, by default, uses the same environment as the Terminal app on Mac.
The shell used defaults to $SHELL on Linux and macOS, PowerShell on
Windows 10 and cmd.exe on earlier versions of Windows. These can be
overridden manually by setting terminal.integrated.shell.* in user
settings.
The default $SHELL on Mac is /bin/bash which uses python for Python2.7. So VS Code will just use the same python to mean Python2.7. When you open a bash shell, it will load your ~/.bash_profile to apply custom aliases and other configurations you added into it.
One solution to your problem is edit your ~/.bash_profile to alias python to python3. But I do not recommend this because this affects all your bash sessions, even those outside of VS Code. This can lead to nasty side effects when you run scripts that need python to be the system Python2.7.
You can instead configure VSCode to load its own aliases, for its own integrated terminal. First, create a file named vscode.bash_profile in your home directory:
$ cat ~/vscode.bash_profile
alias python=$(which python3)
On my env, python3 is Python3.7. You can set it to the what's applicable on your env (ex. maybe python3.7). Then, in VS Code, look for the Terminal shell args setting:
and then open your settings.json and add these lines:
"terminal.integrated.shellArgs.osx": [
"--init-file",
"~/vscode.bash_profile",
]
Finally, restart VS Code. The next time you open the VS Code terminal, python should now be using your Python 3 installation. This should not affect your bash session outside of VS Code.
Note that, if you have some custom settings from the default ~/.bash_profile, you may want to copy it over to your ~/vscode.bash_profile, so that you can still use it on VS Code (ex. changes to PATH, git-completion scripts..).

Ubuntu on Windows as VS Code terminal - executable path

There is a related question but it details the approach for WSL, in newer versions of Windows, the Linux distributions are not beta anymore and they are provided through the MS app store.
Where do I find the path to Ubuntu on Windows executable? (Not WSL.) I tried right-clicking on the icon to find Properties, but it is not there, it seems to be some special kind of a shortcut.
If you have multiple distros installed, you can read this how to launch one individually.
https://msdn.microsoft.com/en-us/commandline/wsl/wsl-config
However if you only have 1 installed, it will pick that one by default
There are three ways to launch and run WSL:
wsl.exe or bash.exe
wsl -c [command] or bash -c [command]
[distro] ie ubuntu -- this is the same as launching the installed app from the Windows menu.
In the first two cases, WSL must pick a distribution to run - a default distribution. If you don't explicitly set a default, it will be the first one installed.
So you can continue to simply use bash.exe
Open VSCode Settings Ctrl + , or File > Prefrences > Settings
User Settings will open up. Add the following in that file:
"terminal.integrated.shell.windows": "C:\Windows\System32\bash.exe"
Save and close the user settings file.
Enjoy bash in VSCode!

How can I redirect linux terminal output to Eclipse console?

I am trying to redirect Terminal output to Eclipse console using external tools where I specified Terminal path (for example /usr/bin/xterm). When I run this tool it opens Terminal outside eclipse. When I did same thing in Windows 7, setting external tool location to Windows\System32\cmd.exe it ran inside Eclipse console. I wonder how I can make Terminal (in Ubuntu 14.04) work the same way.
You should use /bin/sh as command interpreter.
In Windows, cmd.exe combines two functions—interpreting commands and displaying terminal window. In Unix these functions are strictly separated. /bin/sh interprets commands and /usr/bin/xterm (or /usr/bin/x-terminal-emulator) displays terminal window, inside which it runs /bin/sh (or other shell like /bin/bash, /bin/zsh etc.) to interpret commands.
So if you ask Eclipse to run something via /usr/bin/xterm, it will appear in a new window, because that's what XTerm does. If you just want Eclipse to capture the output, ask it to run it directly via /bin/sh.