[python] ImportError: No module named 'selenium'

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'

This question is related to python macos selenium module webdriver

The answer is


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.


pip3 install selenium

Try this if you have python3.


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.


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

sudo pip3 install selenium

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

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


install urllib3

!pip3 install urllib3

import urllib3

than install it

!pip3 install selenium

import selenium

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.


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.


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


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.


Windows:

pip install selenium

Unix:

sudo pip install selenium

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


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


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.


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.


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.


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to selenium

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser Class has been compiled by a more recent version of the Java Environment How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to make Firefox headless programmatically in Selenium with Python? element not interactable exception in selenium web automation Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception?

Examples related to module

How to fix: fatal error: openssl/opensslv.h: No such file or directory in RedHat 7 How to import functions from different js file in a Vue+webpack+vue-loader project Typescript ReferenceError: exports is not defined ImportError: No module named tensorflow ModuleNotFoundError: What does it mean __main__ is not a package? ES6 modules in the browser: Uncaught SyntaxError: Unexpected token import module.exports vs. export default in Node.js and ES6 What's the difference between an Angular component and module Export multiple classes in ES6 modules Python - Module Not Found

Examples related to webdriver

WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception? ImportError: No module named 'selenium' how to use List<WebElement> webdriver How to get attribute of element from Selenium? Selenium Finding elements by class name in python Open web in new tab Selenium + Python When running WebDriver with Chrome browser, getting message, "Only local connections are allowed" even though browser launches properly How can I start InternetExplorerDriver using Selenium WebDriver