ModuleNotFoundError: No module named ‘numpy‘ 解决方案

本文记录了作者在使用Python进行高斯消元算法实现过程中遇到的numpy模块未找到的问题及解决过程。通过检查Python环境、确认numpy安装路径及版本,最终成功解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

今天做 NLA 的作业,需要使用 gaussian elimination,不是特别想用 C++ 来完成,主要是太麻烦。于是就准备使用自己贫瘠的 Python 来完成。

啪啦啪啦打了一堆代码,然后在 VSCode 上运行。我去,什么反馈都没有。蒙了,缓了一口气,让我懵逼的大脑休息一下。切换到 Terminal 来调试。

前面先是 print 的几个语法错误,轻松解决。

File "gaussian_elim.py", line 23
    print A
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(A)?

语法错误解决后,开始大餐。

% python3 gaussian_elim.py
Traceback (most recent call last):
  File "gaussian_elim.py", line 1, in <module>
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

什么意思?没有安装 numpy。好吧。立马 pip3 install numpy,看到系统反馈如下。

% pip3 install numpy
Requirement already satisfied: numpy in /usr/local/lib/python3.8/site-packages (1.19.1)

继续 pyhon3 gaussian_elim.py 还是同样的问题,numpy 包没有找到。这下更懵逼了。

% pip3 list
Package    Version
---------- -------
numpy      1.19.2
pip        20.1.1
protobuf   3.13.0
PyQt5      5.15.0
PyQt5-sip  4.19.24
setuptools 49.2.0
six        1.15.0
TBB        0.1
wheel      0.34.2

这里不是有 numpy 啊。WTF。

认真考虑了几秒,实际是好几分钟,会不会是我机器安装了两个版本 numpy 导致啊。

% sudo python3 -m pip install numpy
Password:
The directory '/Users/yeecall/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/yeecall/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/c1/a9/f04a5b7db30cc30b41fe516b8914c5049264490a34a49d977937606fbb23/numpy-1.19.2-cp37-cp37m-macosx_10_9_x86_64.whl (15.3MB)
    100% |████████████████████████████████| 15.3MB 1.5MB/s 
Installing collected packages: numpy
Successfully installed numpy-1.19.2

验证一下 numpy 是否安装好。

% python3 
Python 3.7.3 (default, Aug  4 2020, 19:30:55) 
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
exit()>>> print (numpy.__version__)
1.19.2

我的天,终于正常了。

问题可能原因

可能是 MBP 上存在两个版本的 Python,即 Python2 和 Python3 导致。

解决方法

指定 python3 安装 numpy 包。使用 python3 -m pip install numpy,注意有权限问题的话,使用 sudo python3 -m pip install numpy。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值