I'm trying to write a script to check a website. It's the first time I'm using selenium. I'm trying to run the script on a OSX system. Although I checked in /Library/Python/2.7/site-packages and selenium-2.46.0-py2.7.egg is present, when I run the script it keeps telling me that there is no selenium module to import.
This is the log that I get when I run my code:
Traceback (most recent call last): File "/Users/GiulioColleluori/Desktop/Class_Checker.py", line 10, in <module> from selenium import webdriver ImportError: No module named 'selenium'
I had a similar problem. It turned out that I had an alias defined for python like so:
alias python=/usr/bin/python3
Apparently virtualenv does not check or update your aliases.
So the solution for me was to remove the alias:
unalias python
Now when I run python, I get the one from the virtual environment. Problem solved.
pip3 install selenium
Try this if you have python3.
While pip install might work. Please check the project structure and see if there is no virtual environment already (It is a good practice to have one) created in the project. If there is, activate it with source <name_of_virtual_env>/bin/activate
(for MacOS) and venv\Scripts\Activate.ps1
(for Windows powershell) or venv\Scripts\activate.bat
(for Windows cmd). then pip install selenium into the environment.
If it isn't,
check if you have a virtual environment with virtualenv --version
If it displays an error, install it with pip install virtualenv
then create a virtual environment with
virtualenv <name_of_virtual_env>
(Both Windows and MacOS)or
python -m venv <name_of_virtual_env>
(Windows Only)
then activate the virtual environment
with
source <name_of_virtual_env>/bin/activate
(for MacOS) and
venv\Scripts\Activate.ps1
(for Windows powershell) or
venv\Scripts\activate.bat
(for Windows cmd).
then install selenium with pip install -U selenium (it will install the latest version).
If it doesn't display an error, just create a virtual environment in the project, activate it and install selenium inside of it.
For python3, on a Mac you must use pip3 to install selenium.
sudo pip3 install selenium
If you are using Anaconda
or Spyder
in windows
, install selenium
by this code in cmd
:
conda install selenium
If you are using Pycharm IDE
in windows
, install selenium
by this code in cmd
:
pip install selenium
Even though the egg file may be present, that does not necessarily mean that it is installed. Check out this previous answer for some hint:
install urllib3
!pip3 install urllib3
import urllib3
than install it
!pip3 install selenium
import selenium
make easy install again by downloading selenium webdriver from its website it is not installed properly.
Edit 1: extract the .tar.gz folder go inside the directory and run python setup.py install from terminal.make sure you have installed setuptools.
Your IDE might be pointing to a different installation of Python than where Selenium is installed.
I'm using Eclipse and when I ran 'quick auto-configure' under:
Preferences > PyDev > Interpreters > Python Interpreter
it pointed to a different version of Python than where pip or easy_install actually installed it.
Selenium worked from the Terminal so I determined which version of python my Terminal was using by running this:
python -c "import sys; print(sys.path)"
then had Eclipse point to that same location, which for me on my 10.11 Mac was here:
/Library/Frameworks/Python.framework/Versions/Current/bin/python2.7/
You can run "Advanced Auto-Config" as well to see all of the installed versions of python and select the one you want to use. When I selected that same location using "Advanced Auto-Config" it finally showed me the Selenium folder as it went through the configuration steps.
If pip isn’t already installed, then first try to bootstrap it from the standard library:
sudo python -m ensurepip --default-pip
Ensure pip, setuptools, and wheel are up to date
sudo python -m pip install --upgrade pip setuptools wheel
Now Install Selenium
sudo pip install selenium
Now run your runner.
Hope this helps. Happy Coding !!
I had the exact same problem and it was driving me crazy (Windows 10 and VS Code 1.49.1)
Other answers talk about installing Selenium, but it's clear to me that you've already did that, but you still get the ImportError: No module named 'selenium'
.
So, what's going on?
Two things:
The solution? You have to install selenium in the same directory to the Python version you're using or change the interpreter to match the directory where Selenium is installed.
In VS Code you change the interpreter here (at the bottom left corner of the screen)
Ready! Now your Python interpreter should find the module.
Windows:
pip install selenium
Unix:
sudo pip install selenium
Navigate to your scripts folder in Python directory (C:\Python27\Scripts) and open command line there (Hold shift and right click then select open command window here). Run pip install -U selenium
If you don't have pip installed, go ahead and install pip first
I had the same problem. Using 'sudo python3 -m pip install selenium' may work.
first you should be sure that selenium is installed in your system.
then install pycharm https://itsfoss.com/install-pycharm-ubuntu/
now if an of packages are not installed it will show red underlines. click on it and install from pycharm.
like for this case click on selenium option in import statement, there you would getting some options. click on install selenium. it will install and automatically run the code successfully if all your drivers are placed in proper directories.
It's 2020 now, use python3 consistently
I meet the same problem when I install selenium using pip3
, but run scripts using python
.
I ran into the same problem with pycharm where my modules wouldn't import after installing them on ubuntu with pip.
If you go File-> Settings -> Project > Python Interpreter
You can click the '+' on the right hand side and import modules into the interpreter.
Not sure if that's your issue but hope this helps.
Source: Stackoverflow.com