ImportError: No module named 'selenium'

PythonMacosSeleniumModuleWebdriver

Python Problem Overview


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 > from selenium import webdriver > ImportError: No module named 'selenium'

Python Solutions


Solution 1 - Python

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!

Solution 2 - Python

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

sudo pip3 install selenium

Solution 3 - Python

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.

Solution 4 - Python

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.

Solution 5 - Python

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

Solution 6 - Python

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

Solution 7 - Python

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.

Solution 8 - Python

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 !!

Solution 9 - Python

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:

https://stackoverflow.com/questions/18868743/how-to-install-selenium-webdriver-on-mac-os

Solution 10 - Python

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.

Solution 11 - Python

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.

Solution 12 - Python

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.

Solution 13 - Python

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

Solution 14 - Python

pip3 install selenium

Try this if you have python3.

Solution 15 - Python

install urllib3

!pip3 install urllib3

import urllib3

than install it

!pip3 install selenium

import selenium

Solution 16 - Python

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.

Solution 17 - Python

Windows:

pip install selenium

Unix:

sudo pip install selenium

Solution 18 - Python

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.

Solution 19 - Python

I was having the same issue when using python 3 with conda distribution, trying to run code on Jupyter in a custom virtualenv. I tried manually installing, installing with pip3, conda etc in anaconda prompt repeatedly but continued to get the import error. Finally solved it by installing it in the Jupyter Tab itself. in Jupyter, in a line, run conda install selenium That's it (If you're facing a similar env that is)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGiulio ColleluoriView Question on Stackoverflow
Solution 1 - PythongffbssView Answer on Stackoverflow
Solution 2 - PythonBrianView Answer on Stackoverflow
Solution 3 - PythonGuzman OjeroView Answer on Stackoverflow
Solution 4 - PythonLebeccaView Answer on Stackoverflow
Solution 5 - PythonHamed BaziyadView Answer on Stackoverflow
Solution 6 - PythontachishView Answer on Stackoverflow
Solution 7 - PythonmindmischiefView Answer on Stackoverflow
Solution 8 - PythonSaif SiddiquiView Answer on Stackoverflow
Solution 9 - PythonSteven CorreiaView Answer on Stackoverflow
Solution 10 - PythonnahurmfView Answer on Stackoverflow
Solution 11 - PythonEhgriezView Answer on Stackoverflow
Solution 12 - Pythonas1992View Answer on Stackoverflow
Solution 13 - PythonRashidView Answer on Stackoverflow
Solution 14 - PythonWimukthi RajapakshaView Answer on Stackoverflow
Solution 15 - PythonSara BourayaView Answer on Stackoverflow
Solution 16 - PythonRohit saiView Answer on Stackoverflow
Solution 17 - PythonGunjan PaulView Answer on Stackoverflow
Solution 18 - Pythoni_m_sanguineView Answer on Stackoverflow
Solution 19 - PythonKriticoderView Answer on Stackoverflow