-2

I have create a virtual environment using

py -m venv C:\Users\HP\Desktop\test\venv
pip3 install numpy
pip3 install matplotlib

But when I run the code it shows ModuleNotFoundError:

enter image description here

But if I run python testing.py in terminal it works, not sure why.

enter image description here

How can I solve this problem?

If using the global enviroment it works fine.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Yummy
  • 11
  • 1
  • 2
    You did not activate the virtual environment in VS Code. – mkrieger1 Mar 29 '23 at 13:22
  • I think I should have selected the corret interpreter path, does it mean it is activated? – Yummy Mar 29 '23 at 13:25
  • Sry i stuck in th estep 3 solution I could not found the Files:Association, and JSON: Schemas section – Yummy Mar 29 '23 at 13:32
  • I think the virtual enviroment should have activated as beside the terminal there is a green (venv)? – Yummy Mar 29 '23 at 13:33
  • I am here to update my progress, I have give up to use VScode for coding Python, I now using pycharm and the enviroment problem solved – Yummy Mar 29 '23 at 16:28

2 Answers2

0

You will need to include the folder containing venv folder when you "Open Folder..". VS Code includes the included venv by default as the virtual environment. This is the simplest method that works for me

  • Yes the venv folder is alrdy in my project folder but it still showing the problem – Yummy Mar 29 '23 at 13:41
  • the python environment is at the bottom right in blue bar. What env are you getting there. can you click on that to see if you change the interpreter? – Aditya Ganguli Mar 29 '23 at 13:49
  • Yes i have seleced the venv i created and the path is .\venv\Scripts\python.ese – Yummy Mar 29 '23 at 13:53
0

Obviously you are using Code Runner to execute the script, which is wrong. You should use the official extension Python to execute the script.

The Select Interpreter panel and show python version on the lower right corner are features of Python extensions. You have selected the interpreter of the virtual environment, which is only valid for Python extensions. And you use Code Runner to execute the script, it will only use the python version configured in the environment variable, so you still get the error.

So you now have two solutions:

  • Choose the correct interpreter and execute the script with the Python extension

    enter image description here

  • Install the numpy and matplotlib package for the interpreter used by Code Runner.

You can verify which interpreter is being used with

import sys
print(sys.executable)
JialeDu
  • 6,021
  • 2
  • 5
  • 24