Easy Meals
Easy Meals

Reputation: 13

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

Upvotes: 1

Views: 6042

Answers (2)

Jill Cheng
Jill Cheng

Reputation: 10344

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.

enter image description here

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:

  1. Please enter "where python" in the cmd window:

    enter image description here

  2. Please check whether there is a python path in the python environment variable:

    enter image description here

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.

Upvotes: 2

Erick Cruz
Erick Cruz

Reputation: 66

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.

Upvotes: 0

Related Questions