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

已安装fake_useragent仍报ModuleNotFoundError的问题求助

问题重现

我已成功下载并反复确认安装了fake_useragent模块,但运行代码时仍报错「ModuleNotFoundError: No module named 'fake_useragent'」。

代码如下:

from fake_useragent import UserAgent
ua = UserAgent()
print(ua.random)

运行输出:

Traceback (most recent call last):
  File "C:\Users\syrbu\PycharmProjects\python selenium\main.py", line 3, in <module>
    from fake_useragent import UserAgent
ModuleNotFoundError: No module named 'fake_useragent'
解决方法
  • 检查Pycharm的Python解释器:打开Pycharm的File > Settings > Project: [你的项目名] > Python Interpreter,查看列表里有没有fake_useragent。如果没有,点击右上角的+,搜索该模块并安装;同时确认当前解释器的路径,和你用pip安装时的Python路径是否一致(可以在cmd里输pip -V看安装路径)。
  • 用项目对应环境的pip安装:如果项目用了虚拟环境,直接在Pycharm底部的Terminal里执行pip install fake_useragent,Terminal默认绑定当前项目的虚拟环境,不会装错地方。
  • 用指定Python版本安装:如果电脑有多个Python版本,别直接用pip,改用python -m pip install fake_useragent(或者python3 -m pip),这样会强制用你当前运行代码的Python版本对应的pip来安装模块。
  • 重启Pycharm:有时候IDE不会实时检测新安装的模块,重启后就能识别到。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 08:25:02