62

I'm trying to write a script to check a website. It's the first time I'm using selenium. I'm trying to run the script on a OSX system. Although I checked in /Library/Python/2.7/site-packages and selenium-2.46.0-py2.7.egg is present, when I run the script it keeps telling me that there is no selenium module to import.

This is the log that I get when I run my code:

Traceback (most recent call last):
  File "/Users/GiulioColleluori/Desktop/Class_Checker.py", line 10, in <module>
    from selenium import webdriver
ImportError: No module named 'selenium'
Dharman
  • 21,838
  • 18
  • 57
  • 107
Giulio Colleluori
  • 961
  • 2
  • 8
  • 14

18 Answers18

85

If you have pip installed you can install selenium like so.

pip install selenium

or depending on your permissions:

sudo pip install selenium

For python3:

sudo pip3 install selenium

As you can see from this question pip vs easy_install pip is a more reliable package installer as it was built to improve easy_install.

I would also suggest that when creating new projects you do so in virtual environments, even a simple selenium project. You can read more about virtual environments here. In fact pip is included out of the box with virtualenv!

gffbss
  • 1,241
  • 15
  • 19
16

For python3, on a Mac you must use pip3 to install selenium.

sudo pip3 install selenium
Brian
  • 410
  • 5
  • 11
8

I had the exact same problem and it was driving me crazy (Windows 10 and VS Code 1.49.1)

Other answers talk about installing Selenium, but it's clear to me that you've already did that, but you still get the ImportError: No module named 'selenium'.

So, what's going on?

Two things:

  1. You installed Selenium in this folder /Library/Python/2.7/site-packages and selenium-2.46.0-py2.7.egg
  2. But you're probably running a version of Python, in which you didn't install Selenium. For instance: /Library/Python/3.8/site-packages... you won't find Selenium installed here and that's why the module isn't found.

The solution? You have to install selenium in the same directory to the Python version you're using or change the interpreter to match the directory where Selenium is installed.

In VS Code you change the interpreter here (at the bottom left corner of the screen) enter image description here

Ready! Now your Python interpreter should find the module.

Guzman Ojero
  • 503
  • 4
  • 10
6

It's 2020 now, use python3 consistently

  • pip3 install selenium
  • python3 xxx.py

I meet the same problem when I install selenium using pip3, but run scripts using python.

Lebecca
  • 1,291
  • 9
  • 20
5

If you are using Anaconda or Spyder in windows, install selenium by this code in cmd:

conda install selenium

If you are using Pycharm IDE in windows, install selenium by this code in cmd:

pip install selenium
hamed baziyad
  • 1,364
  • 3
  • 19
  • 31
4

Your IDE might be pointing to a different installation of Python than where Selenium is installed.

I'm using Eclipse and when I ran 'quick auto-configure' under:

Preferences > PyDev > Interpreters > Python Interpreter

it pointed to a different version of Python than where pip or easy_install actually installed it.

Selenium worked from the Terminal so I determined which version of python my Terminal was using by running this:

python -c "import sys; print(sys.path)"

then had Eclipse point to that same location, which for me on my 10.11 Mac was here:

/Library/Frameworks/Python.framework/Versions/Current/bin/python2.7/

You can run "Advanced Auto-Config" as well to see all of the installed versions of python and select the one you want to use. When I selected that same location using "Advanced Auto-Config" it finally showed me the Selenium folder as it went through the configuration steps.

mindmischief
  • 227
  • 1
  • 9
3

I had the same problem. Using 'sudo python3 -m pip install selenium' may work.

tachish
  • 31
  • 1
3

If pip isn’t already installed, then first try to bootstrap it from the standard library:

sudo python -m ensurepip --default-pip

Ensure pip, setuptools, and wheel are up to date

sudo python -m pip install --upgrade pip setuptools wheel

Now Install Selenium

sudo pip install selenium

Now run your runner.

Hope this helps. Happy Coding !!

Saif Siddiqui
  • 604
  • 1
  • 8
  • 27
2

Even though the egg file may be present, that does not necessarily mean that it is installed. Check out this previous answer for some hint:

How to install Selenium WebDriver on Mac OS

Community
  • 1
  • 1
Steven Correia
  • 461
  • 11
  • 17
2

I had a similar problem. It turned out that I had an alias defined for python like so:

alias python=/usr/bin/python3

Apparently virtualenv does not check or update your aliases.

So the solution for me was to remove the alias:

unalias python

Now when I run python, I get the one from the virtual environment. Problem solved.

nahurmf
  • 41
  • 5
1

make easy install again by downloading selenium webdriver from its website it is not installed properly.

Edit 1: extract the .tar.gz folder go inside the directory and run python setup.py install from terminal.make sure you have installed setuptools.

as1992
  • 53
  • 8
  • I downloaded again selenium from [link](https://pypi.python.org/pypi/selenium)[/link] but I'm not sure how to install it since I've never installed a .tar.gz on a OSX. Previously, I just installed it using "sudo easy_install selenium" from the terminal. Is there any command to install .tar.gz local files? – Giulio Colleluori Jun 30 '15 at 21:15
  • Now it finds the module but it's reporting another issue. I'm gonna try to work it out. Thanks for now! – Giulio Colleluori Jun 30 '15 at 21:44
1

Navigate to your scripts folder in Python directory (C:\Python27\Scripts) and open command line there (Hold shift and right click then select open command window here). Run pip install -U selenium
If you don't have pip installed, go ahead and install pip first

Rashid
  • 57
  • 4
1
pip3 install selenium

Try this if you have python3.

1

install urllib3

!pip3 install urllib3

import urllib3

than install it

!pip3 install selenium

import selenium
  • `!command` isn't valid Python. Maybe you are using Jupyter? In any case, OP doesn't appear to be. Please don't assume the tools people are using. At the very least, please specify the libraries _you_ use. – Chris Apr 26 '20 at 20:06
0

first you should be sure that selenium is installed in your system.

then install pycharm https://itsfoss.com/install-pycharm-ubuntu/

now if an of packages are not installed it will show red underlines. click on it and install from pycharm.

like for this case click on selenium option in import statement, there you would getting some options. click on install selenium. it will install and automatically run the code successfully if all your drivers are placed in proper directories.

Rohit sai
  • 89
  • 8
0

Windows:

pip install selenium

Unix:

sudo pip install selenium
Paul Roub
  • 35,100
  • 27
  • 72
  • 83
Gunjan Paul
  • 315
  • 2
  • 9
0

I ran into the same problem with pycharm where my modules wouldn't import after installing them on ubuntu with pip.

If you go File-> Settings -> Project > Python Interpreter

You can click the '+' on the right hand side and import modules into the interpreter.

Not sure if that's your issue but hope this helps.

0

While pip install might work. Please check the project structure and see if there is no virtual environment already (It is a good practice to have one) created in the project. If there is, activate it with source <name_of_virtual_env>/bin/activate (for MacOS) and venv\Scripts\Activate.ps1 (for Windows powershell) or venv\Scripts\activate.bat (for Windows cmd). then pip install selenium into the environment.

If it isn't, check if you have a virtual environment with virtualenv --version If it displays an error, install it with pip install virtualenv then create a virtual environment with virtualenv <name_of_virtual_env> (Both Windows and MacOS)or

python -m venv <name_of_virtual_env> (Windows Only)

then activate the virtual environment with source <name_of_virtual_env>/bin/activate (for MacOS) and venv\Scripts\Activate.ps1 (for Windows powershell) or venv\Scripts\activate.bat (for Windows cmd). then install selenium with pip install -U selenium (it will install the latest version). If it doesn't display an error, just create a virtual environment in the project, activate it and install selenium inside of it.