skip to Main Content

I have create a virtual environment using

py -m venv C:UsersHPDesktoptestvenv
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.

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search