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

PyCharm中导入Numpy遇ModuleNotFoundError的解决方法咨询

numpy导入报错(ModuleNotFoundError: 'numpy.config' / ImportError: 不能从源码目录导入)的解决方案

错误信息回顾

File "C:\Users\mehth\anaconda3\envs\Traffic Sign Model\lib\numpy\__init__.py", line 126, in <module>
    from numpy.__config__ import show as show_config
ModuleNotFoundError: No module named 'numpy.__config__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Education/Traffic Sign Model/test.py", line 1, in <module>
    import numpy as np
  File "C:\Users\mehth\anaconda3\envs\Traffic Sign Model\lib\numpy\__init__.py", line 131, in <module>
    raise ImportError(msg)
ImportError: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.

Process finished with exit code 1

针对Python 3.6、numpy 1.19.2、conda虚拟环境+PyCharm的环境,提供以下解决方案:

  • 检查运行目录与源码目录冲突

    1. 确认test.py的运行工作目录不是numpy的安装/源码目录:打开PyCharm运行配置,查看"Working directory",确保指向你的项目根目录(如D:/Education/Traffic Sign Model/),而非C:\Users\mehth\anaconda3\envs\Traffic Sign Model\lib\numpy\。
    2. 如果项目目录中存在手动放置的numpy源码文件夹,直接删除该文件夹,避免Python优先从本地目录导入numpy。
  • 修复numpy安装完整性

    1. 打开Anaconda Prompt,激活虚拟环境:conda activate Traffic Sign Model
    2. 卸载当前numpy:conda uninstall numpy -y
    3. 重新安装指定版本:conda install numpy=1.19.2 -y
    4. 命令行验证:执行python -c "import numpy as np; print(np.__version__)",正常输出版本号则说明安装修复完成。
  • 确认PyCharm解释器配置

    1. 进入PyCharm的File > Settings > Project: Traffic Sign Model > Python Interpreter
    2. 确认选中的解释器为目标conda虚拟环境(路径:C:\Users\mehth\anaconda3\envs\Traffic Sign Model\python.exe)
    3. 检查已安装包列表,确保numpy 1.19.2存在;若缺失,点击"+"搜索numpy并安装对应版本。
  • 清理Python缓存文件

    1. 删除项目目录下的__pycache__文件夹
    2. 删除numpy安装目录(C:\Users\mehth\anaconda3\envs\Traffic Sign Model\lib\numpy\)下的__pycache__文件夹
    3. 重启PyCharm后重新运行脚本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 15:46:11