脚本在IDLE可运行但PyCharm报错:ModuleNotFoundError: No module named 'selenium'
ModuleNotFoundError: No module named 'selenium' only in PyCharm (works in IDLE)
Problem Description
I've written a Python script to launch IE and navigate to Google. It runs perfectly in IDLE, but throws a ModuleNotFoundError when executed in PyCharm. Here's my script and the error message:
My Python Script
import selenium from selenium import webdriver driver = webdriver.Ie("c:\\Users\\n0317311\\Desktop\\IEDriverServer.exe") driver.get("http://www.google.co.in")
Error Message in PyCharm
Traceback (most recent call last): File "C:/Users/n0317311/PycharmProjects/Python Programmes/opening browser.py", line 1, in <module> from selenium import webdriver ModuleNotFoundError: No module named 'selenium'
Answer
This is a super common issue, and the root cause is PyCharm and IDLE are using different Python environments:
- IDLE by default uses your system's global Python installation, where you've already installed the
seleniumpackage. - PyCharm automatically creates a separate virtual environment for each new project by default. Since
seleniumisn't installed in this project-specific virtual environment, it throws the module not found error.
Here are two straightforward fixes:
Fix 1: Install selenium in PyCharm's project environment
- Open your project in PyCharm, go to
File > Settings > Project: [Your Project Name] > Python Interpreter(on macOS, it'sPyCharm > Preferences > Project: [Your Project Name] > Python Interpreter) - Click the
+button in the top-right corner of the interpreter list - Search for
seleniumin the package search bar - Select
seleniumfrom the results and clickInstall Package– wait for the installation to finish, then rerun your script
Fix 2: Configure PyCharm to use your global Python interpreter
If you prefer to use the same environment as IDLE:
- Go to the same
Python Interpretersettings page as above - Click the gear icon in the top-right, then select
Add - Choose
System Interpreterfrom the left sidebar - Select the Python executable path that matches what IDLE uses (you can find this path by running
import sys; print(sys.executable)in IDLE) - Click
OKto save, then your project will use the global environment whereseleniumis already installed
内容的提问来源于stack exchange,提问作者Sayanthan
相关产品推荐
相关产品推荐

