PyCharm执行报错ModuleNotFoundError: No module named ‘langdetect‘

文章目录


前言

执行python代码时报错ModuleNotFoundError: No module named 'langdetect',网上找了很多解决方法都不行。

最后反复试了很多可能终于发现是pip 安装过程中断或网络导致文件下载不完整导致的。

以下是大概的解决思路:

解决方法

一开始报错ModuleNotFoundError: No module named 'langdetect'时怀疑是没有下载,执行以下命令

bash 复制代码
(python312) PS D:\file\ai\code\python\test> pip show langdetect
Name: langdetect
Version: 1.0.9
Summary: Language detection library ported from Google's language-detection.
Home-page: https://github.com/Mimino666/langdetect
Author: Michal Mimino Danilak
Author-email: michal.danilak@gmail.com
License: MIT
Location: E:\tool\python\anaconda3\envs\python312\Lib\site-packages
Requires: six
Required-by: unstructured

运行以上命令可以看到已经下载langdetect了,但是检查当前Python环境中安装的langdetect库的版本号时也报错:ModuleNotFoundError: No module named 'langdetect'

bash 复制代码
(python312) PS D:\file\ai\code\python\test> python -c "import langdetect; print(langdetect.__version__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'langdetect'

此时怀疑是不是下载失败了,同时在anaconda3\envs\python312\Lib\site-packages中没有找到langdetect,只找到了langdetect-1.0.9.dist-info,而这个是只存放langdetect元数据信息文件夹。

因此直接强制重装并清除缓存,以下为执行命令:

bash 复制代码
# 1. 彻底卸载
pip uninstall langdetect -y

# 2. 清理 pip 缓存
pip cache purge

# 3. 强制重新安装(不使用缓存,重新下载)
pip install --no-cache-dir --force-reinstall langdetect