ImportError: cannot import name 'webdriver' from 'selenium'
Fixes the selenium import failure where Python finds a selenium module but it has no webdriver inside. Use when `from selenium import webdriver` raises ImportError naming webdriver. Not for `No module named 'selenium'` or chromedriver PATH errors.
You almost certainly have a file named selenium.py in your working directory, and Python imports your file instead of the real package. Rename it (and delete the __pycache__ folder next to it) and the import works. If there is no such file, your selenium install is broken and pip install --force-reinstall selenium fixes it.
The error
ImportError: cannot import name 'webdriver' from 'selenium'Fix it
- Check what Python imported as selenium
python -c "import selenium; print(selenium.__file__)"Expected: a site-packages path ending in selenium/__init__.py. If it prints a path in your project folder ending in selenium.py, that file is the culprit.
- Rename the shadowing file
Rename your selenium.py to something like selenium_demo.py and remove any __pycache__ directory beside it. Then:
python -c "from selenium import webdriver; print('ok')"Expected: prints ok. Python searches the script's directory before site-packages, so your file always won before.
- If there is no shadowing file, reinstall
python -m pip install --force-reinstall seleniumExpected: the import in step 2 now works. A corrupted install can leave a selenium package directory with no webdriver subpackage inside.
When this applies
from selenium import webdriverraises ImportError namingwebdriver- your script lives next to a file called
selenium.py - the same import works in every folder except your project folder
When it does NOT apply
ModuleNotFoundError: No module named 'selenium'means nothing is installed'chromedriver' executable needs to be in PATHhappens after a successful import, at driver startupSessionNotCreatedExceptionis a browser/driver version problem
Compatibility
All selenium 3.x and 4.x. The shadowing behavior is pure Python import semantics and applies to every version.
Why it happens
Python's import system checks the current working directory (the script's folder) before site-packages. A tutorial-style file named selenium.py therefore becomes the selenium module, and since your file defines no webdriver name, the from-import fails. The same trap catches any popular package name used as a script filename.
Edge cases
- Deleting
selenium.pyis not enough if__pycache__/selenium.cpython-312.pycremains. Clear the cache dir too. - Jupyter: a notebook named selenium.ipynb in the working dir does the same thing. Rename it and restart the kernel.
- IDEs sometimes add the project root to sys.path even when you run from elsewhere. The
selenium.__file__check in step 1 sees through that.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.