Home > other >  Can't run script in VScode (module error)
Can't run script in VScode (module error)

Time:08-17

ModuleNotFoundError: No module named 'selenium'

What I tried: Added path to sysdm.cpl Ran multiple commands in terminal in VScode, like creating a venv and path changing.

Note: Selenium is installed and can be found if I run pip --install selenium, yet it still throws an error when I run script in VScode

I have no clue how to run modules and want to start coding.

CodePudding user response:

You have probably not installed selenium for the same python interpreter that is running your script. In your script, put these two lines at the very top:

import sys
print(sys.executable)

Then run this script like you normally would. This should print the path to the interpreter that is running your script, I'll assume it is /path/to/python

Now try running

/path/to/python -m pip install selenium

in your terminal. This should install selenium specifically for the python version that sits at /path/to/python

CodePudding user response:

This is most likely because multiple versions of python exist on your machine, and the environment where you successfully installed the selenium package is not the same python version as the environment you are using.

Solution:

  1. Ctrl Shift P open command palette
  2. search select Python:Select Interpreter enter image description here
  3. Choose the correct interpreter enter image description here

What is the correct interpreter?

  • Select the python environment where the selenium package has been successfully installed

  • Select the python version you want to use, then create a new terminal to activate the environment, and finally install the selenium package for this environment in the new terminal

  • Related