VSCode always runs python interpreter in WSL - python

I selected the windows python interpreter in the top-left section of vscode:
Nonetheless, if I press the run-button on the top-right, a new WSL-Terminal is spawned trying to execute the windows interpreter.
I have the WSL for VSCode Extension installed, but it is not active (as I do not want to run the code using WSL):
Watching the terminal, one can see that the windows interpreter path I preiviously set is tried to be executed by WSL (as this last screenshot shows a bash-prompt) - how can I set this to run with Windows?

Perhaps this is the case because the setting terminal.integrated.shell.windows in the users settings.json was refering to WSL - I was not aware the setting existed before, so lesson learned, I guess.
fix:
"terminal.integrated.shell.windows": "cmd.exe"

Related

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

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/\usr\bin/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.
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".

VSCode uses wrong interpreter

As the title suggest, VSCode is using the wrong interpreter, even though the correct one is listed. As can be seen in the picture, my python script errors out as it cannot find some packages within the environment. After checking which interpreter it is using, it says python 3.8.12 (my base environment). The environment that I am running my code in should be python 3.9.7 (see bottom right of picture).
Now how do I ensure the code runs in the right environment?
I had the same problem for a multi-project workspace. I had to select the interpreter for the whole workspace (the last item "Select at workspace level" in the ctrl-shift-p dialog), then it worked flawlessly.
You can use shortcut keys Ctrl+shift+P to manually adjust the interpreter.

My Visual Studio Code is not using the Python interpreter that I have set it to

I am currently working on setting up pytorch to be used on Visual Studio Code on my Windows 10 machine. Currently I can use it when I am running it on Anaconda prompt. When running on VS Code, I changed the python interpreter to match the one I am using on Anaconda, but when I try to run it is still using the original, and does not recognize "import torch". I am fairly new to using VS Code and running python on Windows, as I usually code in Linux, any help would be much appreciated!
I have attached a photo showing the current environment as well as the result when typing in "python --version" in the command line on VS Code. Screenshot
The reason is that the VSCode terminal is not refreshed and it is still in the previous state.
Solution:
We can use the shortcut key Ctrl+Shift+` (or you can also click "Terminal", "New Terminal") to open a new VSCode terminal, and it will automatically enter the currently selected Python interpreter environment.
In addition, if you need to import and use the module "torch", please install it in the currently selected environment.
Update:
Please check whether Python is available:
Please enter "where python" in the cmd window:
Please check whether there is a python path in the python environment variable:
Generally, for the python global environment, when we enter "python --version" in the terminal, the system will find the first python in the environment variables by default, so it is recommended that you put the commonly used python at the top of the environment variables and restart VSCode.
For conda environment and virtual environment, VSCode terminal will automatically enter the currently selected environment.
Reference: Environment in VScode.
You can click in Python 3.8.. Conda and then vscode shows a list of python interpreters, select your interprete and vscode creates a settings.json in the .vscode folder with the path of your python interpreter in the current folder open in vscode and everytime you open that folder vscode automatically detects your python interpreter.

Python3 seems not to be recognized in VSCode

I had Python version 2.7.17 already installed on my machine. Recently I started coding in Python and I installed VS Code to make things easier. However once I opened VSCode and ran a simple Python Hello World program, VSCode terminal gave a warning saying that
Python 2 support has ended at January 1, 2020
which means the support has already ended and suggested to install the latest Python version. So I heeded the warning and installed Python 3.8.5 on my Windows machine. The installation was completed without any issue. Afterward, when I tried the command python --version on VSCode terminal, it correctly gave the output python 2.7.17. However, when I tried the command python3 --version to check if it was correctly installed, the terminal gave no output.
Since I am a beginner and the support for Python 2 has already ended, I would like to work with Python3 from now on. However, I don't mind Python 2 staying in the machine.
Is this something should I worry about? Or should I uninstall Python 2 completely (When I checked Control Panel both versions are list under currently installed programs)?
According to your description, you can change pythonpath in vscode and still keep python2.
I assume you are using windows system and if not, please let me know. The following is the solution:
open cmd and type: where python. There should be two path, copy it which is about python3.8.5;
press Ctrl+, to open setting.json and add an entry for python.pythonPath manually inside your User Settings:
Now you can press Ctrl+Shift+P and find the option select interpreter. Click it you'll find a interpreter list. After selecting new interpreter, it is necessary to press Ctrl+Shift+` to open a new terminal for further development.
About more detailed information about pythonpath and interpreter you can refer to using python environments in vscode.
The version of Python used depends on your environment, and PATH variable.
I think you need to select the correct Python interpreter. At the bottom of your screen, there should be something that says the Python version number. If you click it, you can get a menu to select the interpreter.
Alternatively, you can hit control-shift-p, type "python interpreter", then click "select python interpreter" option.
Finally, you can alter the default Python interpreter used by changing the User settings in VS Code. Hit control-shift-p, type "open settings", and select Open Settings (JSON). Add a key to the JSON file called "python.pythonPath", so that you have a line that looks something like this:
"python.pythonPath": "full/path/to/the/python/executable/you/want/python.exe",
You might be able to get a hint as to the path to use or available isntallations of python if you type where python or which python (depending on terminal/ OS in use).
Then you can save those settings. Restart VS Code. Hopefully now the correct interpreter is selected by default.

Why my VSCode's interpreter is not working in Mac?

I have VSCode and I am trying to set up a virtual environment. First, I created all the venv properly. When I run in the Terminal is working and activated. However, it is not showing up in the VSCode. Make the matter worse, I can not change my interpreter at all. At first, I had python 2.7 and I could not change into python3 even though everything is set up properly. I fixed that problem with this setting "code-runner.executorMap.python": "python3 -u". And how I cannot change to another interpreter without the delete the setting. I am not sure what to do. Please help.
Thanks
Others have reported setting up virtual environments using VSCode. I suggest you set up your virtual environment using the terminal and activating the environment before starting VSCode. If you are using Anaconda Python this source will instruct you on setting up a virtual environment. For non-anaconda python, there are many pages for doing the same
To change the interpreter in VSCode, go to the View menu and select the Command Palette. When the command palette opens type python in the text box. This will cause commands with python in its name to be displayed. The first command (on my machine) is Python: Select interpreter. Select this and hit return. You will be given a list of python interpreters to choose from.

Categories