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:
But if I run python testing.py
in terminal it works, not sure why.
How can I solve this problem?
If using the global enviroment it works fine.
2
Answers
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
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
Install the
numpy
andmatplotlib
package for the interpreter used by Code Runner.You can verify which interpreter is being used with