Fixing “ImportError: No module named ‘selenium'” in Python

How To Fix No Module Selenium Error In Python

Being able to automate browser testing is an incredibly useful skill for a Python developer. The Selenium Python library allows you to remote control a web browser from a Python script, permitting automation of user actions and assertions on page content.

However, if you are new to Selenium, you may encounter the frustrating ImportError: No module named selenium when trying to get started. In this guide, we will learn:

  • What causes this import error
  • 5 step-by-step methods to fix it
  • How to avoid it when using virtual environments
  • Advanced troubleshooting tips

Equipped with this knowledge, you can confidently use Selenium for test automation in your Python projects.

What Causes the “No Module Named Selenium” Error?

When you attempt to import Selenium in your Python script:

import selenium

You may see an error like:

ImportError: No module named selenium

This error means that Python is unable to locate the Selenium module.

There are a few common reasons why this fails:

  1. Selenium is not installed – You have not yet installed the Selenium package for your Python environment.
  2. Wrong Python version – Selenium may be installed but in a different Python version than the one you are currently running.
  3. Virtual environments – If using a virtual environment, packages need to be installed while the environment is activated.

Essentially, the root cause is that Selenium is not available in the Python environment that is attempting to import it.

Below we will cover 5 different methods to fix this.

Also read: Important Python Selenium Functions You Must Know

Fix 1 – Install Selenium via Pip

The easiest and most common fix is to install Selenium using Python’s pip package manager.

Pip allows you to install third party Python packages from the Python Package Index (PyPI). Fortunately, the main Selenium package is hosted on PyPI, making installation straightforward.

To install or upgrade Selenium, simply open your command prompt, shell, or terminal and enter:

pip install selenium

For Python 3 environments, use pip3 instead:

pip3 install selenium

This automatically fetches the latest selenium package and all its dependencies and installs them into your environment.

Now when you run import selenium it will load from the installed package location rather than failing with an import error.

Some key points about using pip install:

  • Requires internet access to download packages from PyPI
  • Installs packages globally into Python environment
  • Upgrades packages automatically on subsequent calls
  • Easy to call from scripts and continuous integration pipelines

If pip is not available in your environment, first install it with get-pip.py before using it to install selenium.

With pip installing selenium as simple as pip install selenium, it should always be your first method attempted to resolve the no module error.

Fix 2 – Use correct Python version

A common reason for the error persisting even after pip installing selenium is having multiple Python versions configured on your system, with selenium only present in some of them.

For instance you may have installed selenium using:

pip install selenium

But then try to import it in a Python 3 script or interpreter session:

import selenium # Fails with no module error

The fix is to ensure you install selenium for the specific Python version you want to use it in.

For Python 3 environments, invoke pip using the python3 executable rather than plain pip:

python3 -m pip install selenium

You can also check which Python version your default pip is associated to by:

pip -V
# pip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Now when selenium is installed properly for Python 3, you can import it without issues:

import selenium # Success!

So always double check your Python version and use the pip for that version, such as pip3 or python3 -m pip.

Fix 3 – Use Anaconda to Install Selenium

Anaconda and Miniconda are popular Python distributions focused on data science and machine learning.

If your Python environment is managed by Anaconda/Miniconda, it is best to use the conda package manager to install additional packages like selenium.

For example:

conda install selenium

Ensures selenium is installed and available for import across all Anaconda-managed Python environments.

Conda has the advantage of managing environments and dependencies for you automatically. It also allows you to specify exactly which Python version to install a package into.

For instance:

conda create --name my_env python=3.7
conda activate my_env
conda install selenium

Creates a new isolated Miniconda environment called my_env for Python 3.7 then installs selenium into it.

So if using Anaconda/Miniconda distributions, stick to conda for all your package management.

Fix 4 – Install Selenium in Virtual Environments

Python virtual environments provide isolated and self-contained environments in which you can install Python packages without affecting the system-wide packages. Environments need to be activated before installing packages into them:

python3 -m venv my_venv
source my_venv/bin/activate 
pip install selenium

This creates a fresh virtual environment called my_venv, activates it as the current environment, then installs selenium into it.

The key point is that a package needs to be installed while the virtual environment is active in order to be imported from that environment.

So always remember to activate an environment before expected to be able to import packages like selenium that you have installed into it.

Deactivating then reactivating an environment is also useful to force Python to properly detect newly added packages.

Fix 5 – Use Other Python Installation Methods

As well as pip and conda, there are various other ways you can install Python packages like Selenium:

  • Linux package manager – on Ubuntu sudo apt install python3-selenium
  • Custom compiled package – download, compile from source into a wheel file
  • Setuptools – alternative to pip, invoke via easy_install selenium

The key thing is that no matter the installation method, it must install selenium into the currently active Python environment.

If one method fails with the no module error, try an alternative and observe if it resolves the problem.

Recap of Selenium Installation Fixes

Here is a recap of the key fixes covered to tackle the “No module named selenium” error:

FixCommandNotes
pip installpip install seleniumEasy fix for all Python versions
Check Python versionpython3 -m pip install seleniumEnsure package matches environment
Use Conda (Anaconda)conda install seleniumConda manages packages & environments
Install in virtual environmentspip install selenium (inside activated env)Environments isolate package installations
Other methodseasy_install, linux packagesPiper alternatives to try

Conclusion

As you have seen, there are several straightforward methods to fix the common No module named 'selenium' import error. In most cases, simply running pip install selenium is all that is required. However, issues can occur if you have multiple Python versions configured or make use of virtual environments. So take care and install selenium into the specific active Python environment that you wish to import it from.

Now that you understand the key fixes and how to avoid issues with virtual environments, you can confidently automate browser testing using Selenium Python!