Skip to content

How to Resolve "ModuleNotFoundError: No module named selenium" in Python

Fix the 'ModuleNotFoundError: No module named selenium' by installing the package correctly in your Python environment, troubleshooting virtual environments, and resolving IDE configuration issues.

The ModuleNotFoundError: No module named 'selenium' is a frequent error encountered when working with the Selenium library for browser automation in Python. It means the Python interpreter can not find the selenium library upon import. This usually indicates the package isn't installed in the Python environment you're using.

This guide provides step-by-step solutions to install selenium correctly and resolve this error.

Understanding the Error

This error signifies that when Python executes import selenium or from selenium import ..., it searches its known package locations but can not find the selenium library.

Solution 1: Install selenium using pip (Most Common)

selenium is a third-party library and must be installed using pip, Python's package installer.

Open your terminal or command prompt and run:

pip install selenium

Variations (Choose the one appropriate for your setup):

pip3 install selenium

python -m pip install selenium
python3 -m pip install selenium

py -m pip install selenium

Managing WebDrivers (webdriver-manager)

Selenium requires a specific browser driver (like ChromeDriver for Chrome, GeckoDriver for Firefox) to interact with the browser. While you can download these manually, the webdriver-manager package automates this process. It's highly recommended.

Install it alongside or after Selenium:

pip install webdriver-manager

Then, use it in your code (example with Chrome):

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager # Import manager

try:
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    driver.get("https://www.python.org")
    print(f"Page title: {driver.title}")
    driver.quit() # Close the browser and driver process
except Exception as e:
    print(f"An error occurred: {e}")

Troubleshooting Common Installation Problems:

If pip install selenium (or webdriver-manager) fails or the ModuleNotFoundError persists, check these points:

  • Using the Correct pip: Ensure you're using the pip linked to the Python interpreter running your script (usually pip3 or python3 -m pip for Python 3). Verify versions with python --version and pip --version (or pip3 --version).
  • Working with Virtual Environments (Crucial!): This is the most frequent reason for the error. You likely installed selenium globally but are running your script inside a virtual environment (or vice-versa).
    • Activate your virtual environment before installing:
      python3 -m venv venv
      source venv/bin/activate       # Linux/macOS
      .\venv\Scripts\Activate.ps1   # Windows PowerShell
      .\venv\Scripts\activate.bat    # Windows CMD
      pip install selenium webdriver-manager
    • Confirm your IDE (VS Code, PyCharm) uses the Python interpreter from your activated virtual environment.
  • Permissions Issues (--user or sudo): When installing globally (not recommended).
    • Recommended: Install for the current user: pip install selenium webdriver-manager --user
    • Use Cautiously: Use admin/root privileges: sudo pip install selenium webdriver-manager (Linux/macOS) or Run Terminal as Administrator (Windows). Virtual environments avoid this need.
  • Multiple Python Installations / IDE Configuration: Double-check that the Python interpreter selected in your IDE matches the one you used pip with. Use the "Select Interpreter" feature in your IDE.
  • Naming Conflicts (selenium.py): Ensure none of your project files are named selenium.py. This "shadows" the installed package. Rename your file if necessary. Avoid using selenium as a variable name before the import.

Verifying Installation (pip show)

Confirm the package is installed in the active environment:

pip show selenium
pip show webdriver-manager

These commands should display package details if successful. "Package(s) not found" means it's not installed in that specific environment.

Platform/Tool Specific Installation Notes:

  • Windows: Use CMD or PowerShell. py -m pip install selenium webdriver-manager is often reliable. Run as Administrator for global installs if needed (prefer venvs/--user).
  • macOS / Linux: Use the Terminal. Use pip3 or python3 -m pip. Use sudo cautiously for global installs. You might need system packages for browsers/drivers depending on your setup.
  • VS Code: Use the integrated terminal (Ctrl + `` ). Ensure the correct Python interpreter (often venv) is selected.
  • PyCharm: Use the Terminal (Alt+F12) or the Python Packages tool. Ensure the correct project interpreter is set.
  • Anaconda: Use the Anaconda Prompt/Navigator or terminal in an active conda environment.
    conda install -c conda-forge selenium webdriver-manager # Preferred in Conda
  • Jupyter Notebook: Install from a notebook cell:
    !pip install selenium webdriver-manager
    Restart the kernel after installation.

Using selenium After Installation

Once installed, import and use the necessary components:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

try:
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

    driver.get("https://www.google.com")
    print(f"Opened page with title: {driver.title}")


    driver.quit()

except ImportError:
    print("Error: 'selenium' or 'webdriver-manager' module not found.")
    print("Please install them using: pip install selenium webdriver-manager")
except Exception as e:
    print(f"An error occurred: {e}")

Linters like Pylance (in VS Code) might show this warning even if selenium is installed. This usually means:

  1. Incorrect Interpreter Selected: The IDE isn't pointed to the Python environment where selenium resides. Use the "Python: Select Interpreter" command (Ctrl+Shift+P) to choose the correct one (especially the venv interpreter).
  2. IDE Needs Restart: Restarting your IDE often resolves temporary linter glitches after package installation.
  3. Linter Glitch (Workaround): If certain the installation is correct and code runs, suppress the specific warning as a last resort:
    import selenium  # type: ignore

Conclusion

The ModuleNotFoundError: No module named 'selenium' indicates that the selenium package is missing from your Python environment.

  • The primary solution is pip install selenium.
  • Crucially, ensure you're installing into the correct environment, preferably using a virtual environment, and that your IDE is configured to use that same environment.
  • Installing webdriver-manager is also highly recommended for easier browser driver management.
Navigation