I’m encountering an issue while trying to run a Python file in Visual Studio Code (VSCode) within a Conda environment. The error message states “ModuleNotFoundError: No module named ‘torch'”. What’s puzzling is that VSCode’s interpreter recognizes the ‘torch’ module without any errors (the import torch
statement is highlighted in green). However, when I try to run the code using the “Run Code” feature in VSCode, I encounter this error. Interestingly, running the Python file from the terminal or using the “Run Python File” option in VSCode works perfectly fine.
Here are some additional details:
- The interpreter in VSCode is correctly set to …anaconda3python.exe.
- I installed the libraries using the following commands:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
pip install torch torchvision torchaudio
- Both
pip list
andconda list
show that the necessary libraries are installed within the Conda environment. - When I run
where python
, I get the following paths:C:Usersgiovaanaconda3python.exe
C:UsersgiovaAppDataLocalMicrosoftWindowsAppspython.exe
- Running
where python3
returnsC:UsersgiovaAppDataLocalMicrosoftWindowsAppspython3.exe
. I’m unsure whether I should also have ‘python3’ within the Anaconda environment.
I’m seeking insights into why this discrepancy occurs and potential solutions to resolve it. Any ideas would be greatly appreciated!