1

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

When I do this, it still says 3.8.6 for the version, but I do notice it has a different error than when I select the actual 3.8.6 interpreter. I have also installed torch into that environment, and verified it in the anaconda prompt. Neither 'conda activate base' or 'conda init cmd.exe' seem to be working
@ Easy Meals -The reason is still that the VSCode terminal does not enter the required environment. I updated my answer and you could refer to it.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.