0

I know this has been talked about before but I cant seem to find the correct way to make it work.

I have a python script that imports the selenium module (opens a website, sleep for 3 sec and closes it). When I run it with Pycharm everything runs great but when I try to run it from CMD (the ultimate goal is to run it in Jenkins but that is not relevant right now) it gives an error that it cannot find the 'selenium' module.

ModuleNotFoundError: No module named 'selenium'

I read that I have to run the command

python -m

but I dont really know where to point it to. I tried to take the interpreter directory from Pycharm and run

python -m C:\Users\XXX\PycharmProjects\untitled\venv\Scripts | script.py

but it still gave the same issue.

Can someone please point me in the right direction about this command syntax?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
jackhammer
  • 87
  • 10
  • (no sudo) pip freeze | grep selenium. If no output, (no sudo) pip install selenium (in you virtual envoirement) – Wonka Oct 17 '19 at 19:46
  • 1
    No @Wonka, _please_ don't install libraries with `sudo pip`! Keep OS-level packages and environment-specific packages separate. Install a system-level package or use `pip` _within a virtual environment_ or maybe with `--user`. – ChrisGPT was on strike Oct 17 '19 at 19:48
  • @Chris probably thats the reason of my problems sometimes – Wonka Oct 17 '19 at 19:49
  • Possible duplicate of [ImportError: No module named 'bottle' - PyCharm](https://stackoverflow.com/questions/26069254/importerror-no-module-named-bottle-pycharm) – ChrisGPT was on strike Oct 17 '19 at 19:50
  • (I'm aware that Selenium and Bottle are different things, but that's not really relevant.) – ChrisGPT was on strike Oct 17 '19 at 19:50
  • Either I'm missing something or you guys misunderstood me. The error I'm getting is when I run it under CMD. Under Pycharm it runs ok. I need to run the script from CMD – jackhammer Oct 17 '19 at 20:15
  • Then why did you tag it with [tag:pycharm]? Either way, there are tons of questions about installing libraries already. If your situation is different please explain how. Tell us what you've already tried and why it didn't work. – ChrisGPT was on strike Oct 18 '19 at 00:55
  • but i dont need to install the module. I already installed it in pycharm but when running the .py from cmd it doesnt know where to find the selenium module... this is what i need help with. how do i tell him where to import the selenium module from? – jackhammer Oct 18 '19 at 07:35
  • BTW, this only happens with modules i add to pycharm. if i import 'sys' the script run perfectly from cmd, so it knows where to import 'sys' from. – jackhammer Oct 18 '19 at 07:38
  • If you need a library in a specific environment you need to install it _in_ that environment. Pycharm likely maintains its own environments. Use the environment it created (look in your settings for "interpreter" and use that path when running Python) or install the library into the environment you're using. (`sys` is included in the standard library, so it's not a good point of comparison.) – ChrisGPT was on strike Oct 18 '19 at 14:13

1 Answers1

0

This error message while executing the python script from CLI...

ModuleNotFoundError: No module named 'selenium'

...implies that Selenium wasn't installed in your system harddisk, rather Selenium was installed with 's virtual environment, i.e. within:

C:\Users\XXX\PycharmProjects\untitled\venv

which your system is unaware of.


Solution

To execute the python script from CLI you need to install Selenium in your system using either of the following commands:

  • Using pip (install):

    pip install selenium
    
  • Using pip3 (install or upgrade):

    pip3 install -U selenium
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352