Python Selenium import error

I am trying to execute some python project and I am getting following error:-

File "feed.py", line 17, in <module> from selenium import webdriver
ImportError: No module named selenium

I installed selenium package by using pip command for both python2 and python3. If I run python on terminal, I don't get this error if I try to import selenium.

But project I am executing using venv. I am unable to figure out from where it is trying to look for selenium package.

When I execute python2.7/python/python on terminal and run from selenium import webdriver, I don't see any import errors. But the project when I execute is giving me error. I am unable to locate the path which is being looked up for selenium. How can I find it?

2

3 Answers

You may like to create a virtual environment for Selenium, which is okay, so, do as mentioned earlier. But I think the following is needed to install Selenium with Python on Ubuntu.

Since you are on Python2, so install Selenium like this:

sudo apt-get install python-selenium # for Python2

And, if you want to move to Python3, then replace python-selenium with python3-selenium in the above command. Also, do remember to download Geckodriver for Firefox to further work with Selenium.

wget 

You would then need to extract the Geckodriver to a folder such as ~/.local/bin for setting it on your execution PATH.

First activate the virtual environment:

$ source venv/bin/activate

... tthen your terminal will look like this:

(venv)$

Then install selenium:

(venv) $ pip install selenium

Then you will have selenium installed inside the venv.

The next time when you open a new terminal, just activate the venv and run your code.

4

Instead of importing webdriver you can import your preferred browser from selenium.webdriver; in my case import Firefox:

from selenium.webdriver import Firefox
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like