Executing python script with selenium with batch (selenium error)

Question:

I’m trying to execute a python script with selenium module via batch file.
The python script itself runs perfectly OK, but when I try to execute the script through a .bat file it gives me the error ‘ModuleNotFoundError: No module named ‘selenium”

from selenium import webdriver

driver = webdriver.Chrome(executable_path='C:/Temp/chromedriver.exe')

driver.get('http://www.example.com')
C:PythonPython37python.exe C:PythonTesttestFile.py

The error printed is:

Traceback (most recent call last):
  File "C:UsersElGregoryPycharmProjectsPythonTesttestFile.py", line 1, in <module>
    from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'

Which obviously is a Python error, but when the code is run in Pycharm it runs as expected. (=selenium installed correctly)

Any help apreciated.

Asked By: ElGregory

||

Answers:

This could be because when you are running inside Pycharm, the libraries are installed in your virtual python environment (venv).

Either activate the virtual environment before running the python file for which you can read more at https://docs.python.org/3/library/venv.html

Or install your libraries globally

Answered By: work_ishaan

Use the following command, you most probably don’t have selenium installed.

pip install -U selenium
Answered By: Ritvij Srivastav