Reputation: 649
I am new to python. I am using VS code on Windows. Following is my simple python code.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
I am trying to install selenium package but it is giving me error ModuleNotFoundError: No module named 'selenium'. I tried following commands.
conda install selenium
python -m pip install -U selenium
Then I tried in a virtual env
conda create -n selenium-env python=3.6
conda activate selenium-env
conda install -n selenium0env selenium
Still getting the same error. Update:
I noticed that when I first run the py file after restarting VS code, it activates one of my virtual environments automatically. I think that virtual env does not have selenium installed in it. Below is the terminal output when I run the py file
How do I stop that environment from getting activated automatically? I want to run this in a different environment.
PS D:\Learnings\Python\SampleDesktopApp> conda activate pyfinance
PS D:\Learnings\Python\SampleDesktopApp> &
C:/Users/rajen/anaconda3/envs/pyfinance/python.exe
d:/Learnings/Python/SampleDesktopApp/Test.py
Traceback (most recent call last):
File "d:\Learnings\Python\SampleDesktopApp\Test.py", line 1, in <module>
from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'
PS D:\Learnings\Python\SampleDesktopApp>
Upvotes: 0
Views: 488
Reputation: 649
I was installing the selenium in one environment and running py file in another. So I used VS code option Python: Select Interpreter to change the environment while running the file. It worked.
Upvotes: 0
Reputation: 24
Well first things first update your pip.
Use the command: python -m pip install --upgrade pip if that doesn't work then there is a chance there's a bug in your Python or site-packages directory. Uninstall Python and then reinstall the latest version and it should work then.
Side note: Once you get selenium working, you can't just initialize Firefox or Chrome without having their respective drivers. for Firefox its geckodriver.exe https://github.com/mozilla/geckodriver/releases for Chrome its chromedriver.exe https://chromedriver.chromium.org/downloads
Once you have the driver installed you have to include the driver in the Webdriver argument executable_path like so:
driver = webdriver.Firefox(executable_path="drivers/geckodriver.exe")
Update
Conda is automatically running the virutalenv so instead of using command conda activate pyfinance.
try running with just simple: python pyfinance.py
Upvotes: 1