{"id":57228,"date":"2023-11-27T22:23:42","date_gmt":"2023-11-27T22:23:42","guid":{"rendered":"https:\/\/www.askpython.com\/?p=57228"},"modified":"2025-04-10T20:52:36","modified_gmt":"2025-04-10T20:52:36","slug":"fix-importerror-selenium","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/fix-importerror-selenium","title":{"rendered":"Fixing &#8220;ImportError: No module named &#8216;selenium'&#8221; in Python"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>However, if you are new to Selenium, you may encounter the frustrating <strong>ImportError: No module named selenium<\/strong> when trying to get started. <strong>In this guide, we will learn:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What causes this import error<\/li>\n\n\n\n<li>5 step-by-step methods to fix it<\/li>\n\n\n\n<li>How to avoid it when using virtual environments<\/li>\n\n\n\n<li>Advanced troubleshooting tips<\/li>\n<\/ul>\n\n\n\n<p>Equipped with this knowledge, you can confidently use <a href=\"https:\/\/www.askpython.com\/python-modules\/introduction-browser-drivers-selenium\" data-type=\"post\" data-id=\"27218\"><strong>Selenium<\/strong><\/a> for test automation in your Python projects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Causes the &#8220;No Module Named Selenium&#8221; Error?<\/h2>\n\n\n\n<p>When you attempt to import Selenium in your Python script:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport selenium\n<\/pre><\/div>\n\n\n<p>You may see an error like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nImportError: No module named selenium\n<\/pre><\/div>\n\n\n<p>This error means that Python is unable to locate the Selenium module.<\/p>\n\n\n\n<p>There are a few common reasons why this fails:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Selenium is not installed<\/strong> &#8211; You have not yet installed the Selenium package for your Python environment.<\/li>\n\n\n\n<li><strong>Wrong Python version<\/strong> &#8211; Selenium may be installed but in a different Python version than the one you are currently running.<\/li>\n\n\n\n<li><strong>Virtual environments<\/strong> &#8211; If using a virtual environment, packages need to be installed while the environment is activated.<\/li>\n<\/ol>\n\n\n\n<p>Essentially, the root cause is that Selenium is not available in the Python environment that is attempting to import it.<\/p>\n\n\n\n<p>Below we will cover 5 different methods to fix this.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/important-python-selenium-functions\" data-type=\"post\" data-id=\"26567\">Important Python Selenium Functions You Must Know<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 1 &#8211; Install Selenium via Pip<\/h2>\n\n\n\n<p>The easiest and most common fix is to install Selenium using <a href=\"https:\/\/www.askpython.com\/python-modules\/python-pip\" data-type=\"post\" data-id=\"3848\">Python&#8217;s pip package manager<\/a>.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>To install or upgrade Selenium, simply open your command prompt, shell, or terminal and enter:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip install selenium\n<\/pre><\/div>\n\n\n<p>For Python 3 environments, use pip3 instead:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip3 install selenium\n<\/pre><\/div>\n\n\n<p>This automatically fetches the latest selenium package and all its dependencies and installs them into your environment.<\/p>\n\n\n\n<p>Now when you run <code>import selenium<\/code> it will load from the installed package location rather than failing with an import error.<\/p>\n\n\n\n<p>Some key points about using pip install:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Requires internet access to download packages from PyPI<\/li>\n\n\n\n<li>Installs packages globally into Python environment<\/li>\n\n\n\n<li>Upgrades packages automatically on subsequent calls<\/li>\n\n\n\n<li>Easy to call from scripts and continuous integration pipelines<\/li>\n<\/ul>\n\n\n\n<p>If pip is not available in your environment, first install it with <code>get-pip.py<\/code> before using it to install selenium.<\/p>\n\n\n\n<p>With pip installing selenium as simple as <code>pip install selenium<\/code>, it should always be your first method attempted to resolve the no module error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 2 &#8211; Use correct Python version<\/h2>\n\n\n\n<p>A common reason for the error persisting even after pip installing selenium is <strong>having multiple Python versions configured on your system<\/strong>, with selenium only present in some of them.<\/p>\n\n\n\n<p><strong>For instance you may have installed selenium using:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip install selenium\n<\/pre><\/div>\n\n\n<p><strong>But then try to import it in a Python 3 script or interpreter session:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport selenium # Fails with no module error\n<\/pre><\/div>\n\n\n<p>The fix is to <strong>ensure you install selenium for the specific Python version<\/strong> you want to use it in.<\/p>\n\n\n\n<p>For Python 3 environments, invoke pip using the python3 executable rather than plain <code>pip<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npython3 -m pip install selenium\n<\/pre><\/div>\n\n\n<p>You can also check which Python version your default pip is associated to by:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip -V\n# pip 21.2.4 from \/usr\/local\/lib\/python3.9\/site-packages\/pip (python 3.9)\n<\/pre><\/div>\n\n\n<p>Now when selenium is installed properly for Python 3, you can import it without issues:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport selenium # Success!\n<\/pre><\/div>\n\n\n<p>So always double check your Python version and use the pip for that version, such as <code>pip3<\/code> or <code>python3 -m pip<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 3 &#8211; Use Anaconda to Install Selenium<\/h2>\n\n\n\n<p>Anaconda and Miniconda are popular Python distributions focused on data science and machine learning.<\/p>\n\n\n\n<p>If your Python environment is managed by Anaconda\/Miniconda, it is best to use the <strong>conda<\/strong> package manager to install additional packages like selenium.<\/p>\n\n\n\n<p><strong>For example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nconda install selenium\n<\/pre><\/div>\n\n\n<p>Ensures selenium is installed and available for import across all Anaconda-managed Python environments.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><strong>For instance:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nconda create --name my_env python=3.7\nconda activate my_env\nconda install selenium\n<\/pre><\/div>\n\n\n<p>Creates a new isolated Miniconda environment called <code>my_env<\/code> for Python 3.7 then installs selenium into it.<\/p>\n\n\n\n<p>So if using Anaconda\/Miniconda distributions, stick to conda for all your package management.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 4 &#8211; Install Selenium in Virtual Environments<\/h2>\n\n\n\n<p><strong>Python virtual environments<\/strong> 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:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npython3 -m venv my_venv\nsource my_venv\/bin\/activate \npip install selenium\n<\/pre><\/div>\n\n\n<p>This creates a fresh virtual environment called <code>my_venv<\/code>, activates it as the current environment, then installs selenium into it.<\/p>\n\n\n\n<p>The key point is that <strong>a package needs to be installed while the virtual environment is active<\/strong> in order to be imported from that environment.<\/p>\n\n\n\n<p>So always remember to activate an environment before expected to be able to import packages like selenium that you have installed into it.<\/p>\n\n\n\n<p>Deactivating then reactivating an environment is also useful to force Python to properly detect newly added packages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix 5 &#8211; Use Other Python Installation Methods<\/h2>\n\n\n\n<p>As well as pip and conda, there are various other ways you can install Python packages like Selenium:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Linux package manager<\/strong> &#8211; on Ubuntu <code>sudo apt install python3-selenium<\/code><\/li>\n\n\n\n<li><strong>Custom compiled package<\/strong> &#8211; download, compile from source into a wheel file<\/li>\n\n\n\n<li><strong>Setuptools<\/strong> &#8211; alternative to pip, invoke via <code>easy_install selenium<\/code><\/li>\n<\/ul>\n\n\n\n<p>The key thing is that no matter the installation method, it must install selenium into the currently active Python environment.<\/p>\n\n\n\n<p>If one method fails with the no module error, try an alternative and observe if it resolves the problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Recap of Selenium Installation Fixes<\/h2>\n\n\n\n<p>Here is a recap of the key fixes covered to tackle the &#8220;No module named selenium&#8221; error:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Fix<\/th><th>Command<\/th><th>Notes<\/th><\/tr><\/thead><tbody><tr><td>pip install<\/td><td><code>pip install selenium<\/code><\/td><td>Easy fix for all Python versions<\/td><\/tr><tr><td>Check Python version<\/td><td><code>python3 -m pip install selenium<\/code><\/td><td>Ensure package matches environment<\/td><\/tr><tr><td>Use Conda (Anaconda)<\/td><td><code>conda install selenium<\/code><\/td><td>Conda manages packages &amp; environments<\/td><\/tr><tr><td>Install in virtual environments<\/td><td><code>pip install selenium<\/code> (inside activated env)<\/td><td>Environments isolate package installations<\/td><\/tr><tr><td>Other methods<\/td><td><code>easy_install<\/code>, linux packages<\/td><td>Piper alternatives to try<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>As you have seen, there are several straightforward methods to fix the common <code>No module named 'selenium'<\/code> import error. In most cases, simply running <code>pip install selenium<\/code> 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. <\/p>\n\n\n\n<p>Now that you understand the key fixes and how to avoid issues with virtual environments, you can confidently automate browser testing using Selenium Python!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":77,"featured_media":64172,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-57228","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=57228"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57228\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/64172"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=57228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=57228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=57228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}