You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

脚本在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 selenium package.
  • PyCharm automatically creates a separate virtual environment for each new project by default. Since selenium isn'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

  1. Open your project in PyCharm, go to File > Settings > Project: [Your Project Name] > Python Interpreter (on macOS, it's PyCharm > Preferences > Project: [Your Project Name] > Python Interpreter)
  2. Click the + button in the top-right corner of the interpreter list
  3. Search for selenium in the package search bar
  4. Select selenium from the results and click Install 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:

  1. Go to the same Python Interpreter settings page as above
  2. Click the gear icon in the top-right, then select Add
  3. Choose System Interpreter from the left sidebar
  4. Select the Python executable path that matches what IDLE uses (you can find this path by running import sys; print(sys.executable) in IDLE)
  5. Click OK to save, then your project will use the global environment where selenium is already installed

内容的提问来源于stack exchange,提问作者Sayanthan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:10:41