Python 3.9中出现ModuleNotFoundError: No module named 'scipy'错误

5

我正在Ubuntu 18.04上运行Python 3.9。我已经执行了命令sudo apt-get install python-scipy,并收到了以下信息:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-scipy is already the newest version (0.19.1-2ubuntu1).
The following packages were automatically installed and are no longer required:
  linux-hwe-5.4-headers-5.4.0-42 linux-hwe-5.4-headers-5.4.0-53
  linux-hwe-5.4-headers-5.4.0-56 linux-hwe-5.4-headers-5.4.0-58
  linux-hwe-5.4-headers-5.4.0-59 linux-hwe-5.4-headers-5.4.0-60
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 12 not upgraded.

然而,当我尝试运行使用 from scipy import integrate 的 python3.9 代码时,出现错误:

ModuleNotFoundError: No module named 'scipy'

我已经阅读了这篇帖子并尝试使用以下命令卸载和安装scipy:

sudo apt-get install python3-scipy

但这并没有起作用。有什么建议吗?

编辑 1:我尝试了sudo pip3 install scipy,它产生了以下信息:

The directory '/home/nick/.cache/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 '/home/nick/.cache/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.

然而,当我尝试再次运行该代码时,仍然出现相同的ImportError错误。


尝试在pip3之前添加-H。 - DapperDuck
Requirement already satisfied: scipy in /usr/lib/python3/dist-packages - Woj
运行以下命令:pip freeze | grep scipy。它返回什么? - DapperDuck
3个回答

6
也许可以尝试。
python3.9 -m pip install scipy --user

这将使用Python3.9的pip在没有sudo权限的情况下将包安装到指定位置。


这是正确的答案。无论是使用python -mpip,还是用户安装(如果不使用虚拟环境)。 - ev-br
Requirement already satisfied: scipy in /usr/local/lib/python3.9/site-packages (1.7.3) Requirement already satisfied: numpy<1.23.0,>=1.16.5 in /usr/local/lib/python3.9/site-packages (from scipy) (1.21.4) but still get this: ModuleNotFoundError: No module named 'scipy' - Dr.jacky
嗨@Dr.jacky,你的问题很可能是pippython之间不匹配。如果我是你,我会检查当前的python是否确实是安装在/usr/local/bin中的python3.9。有时系统中可能会有多个python3.9,这会使事情变得非常难以处理... - Yang Yushi

1
使用pipwin。
pip install pipwin

下载后

pipwin install scipy

2
虽然这段代码可能提供了问题的解决方案,但最好添加上为什么/如何运作的上下文。这可以帮助未来的用户学习并将该知识应用到他们自己的代码中。当代码被解释时,您还可能会得到用户的积极反馈/赞同。 - Melanie Shebel

0

尝试执行pip3 install scipy,如果返回ERRNO 13: access denied错误,则尝试执行pip3 install scipy --user


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接