A common error you may encounter when using Python is modulenotfounderror: no module named ‘selenium’.
This error occurs when the Python interpreter cannot detect the Selenium library in your current environment.
This tutorial goes through the exact steps to troubleshoot this error for the Windows, Mac and Linux operating systems.
Table of contents
ModuleNotFoundError: no module named ‘selenium’
What is Selenium?
Selenium is a suite of tools for automating web browsers. You can use Selenium to automate web applications for testing purposes, though it is not only for testing.
The simplest way to install Selenium is to use the package manager for Python called pip. The following installation instructions are for the major Python version 3.
How to Install Selenium on Windows Operating System
First, you need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.
You can check your Python version with the following command:
python3 --version
You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.
python get-pip.py
You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.
pip --version
Selenium installation on Windows Using pip
To install Selenium, run the following command from the command prompt.
pip3 install selenium
How to Install Selenium on Mac Operating System using pip
Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3:
python3 --version
Python 3.8.8
Download pip by running the following curl command:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
The curl command allows you to specify a direct download link. Using the -o option sets the name of the downloaded file.
Install pip by running:
python3 get-pip.py
From the terminal, use pip3 to install Selenium:
pip3 install selenium
How to Install Selenium on Linux Operating Systems
All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.
Installing pip for Ubuntu, Debian, and Linux Mint
sudo apt install python-pip3
Installing pip for CentOS 8 (and newer), Fedora, and Red Hat
sudo dnf install python-pip3
Installing pip for CentOS 6 and 7, and older versions of Red Hat
sudo yum install epel-release
sudo yum install python-pip3
Installing pip for Arch Linux and Manjaro
sudo pacman -S python-pip
Installing pip for OpenSUSE
sudo zypper python3-pip
Selenium installation on Linux with Pip
Once you have installed pip, you can install Selenium using:
pip3 install selenium
Installing Selenium Using Anaconda
First, to create a conda environment to install PIL.
conda create -n selenium python=3.6
Then activate the selenium container. You will see “selenium” in parentheses next to the command line prompt.
source activate selenium
Now you’re ready to install Selenium using conda.
Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda and created your conda environment, you can install Selenium using one of the following commands:
conda install -c conda-forge selenium
Check Selenium Version
Once you have successfully installed Selenium, you can check the version of Selenium. If you used pip to install Selenium, you can use pip show from your terminal.
pip show selenium
Name: selenium
Version: 4.1.0
Second, within your python program, you can import selenium and then reference the __version__ attribute:
import selenium
print(selenium.__version__)
4.1.0
If you used conda to install Selenium, you could check the version using the following command:
conda list -f selenium
# Name Version Build Channel
selenium 3.141.0 py36hfa26744_1002 conda-forge
Summary
Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you do not have the module installed in your Python environment, you can use pip to install the package. However, you must ensure you have pip installed on your system. You can also install Anaconda on your system and use the conda install command to install Selenium.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
For further reading on missing modules in Python, go to the article: How to Solve Python ModuleNotFoundError: no module named ‘urllib2’.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.