【发布时间】:2013-04-05 15:51:24
【问题描述】:
我已经通过执行在 ubuntu 上安装了 numpy
sudo apt-get install python-numpy
在终端上执行时出现此错误。
>>> import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>
为什么会这样?我通过互联网尝试了很多次,但我找不到解决方案。请问您能告诉我如何解决这个问题吗?
编辑1: 我知道我必须使用 pip 为我在终端上运行的 python 版本安装 numpy。当我在终端上输入“python”时,Python 2.7.3 运行。所以这意味着我必须为 python 2.7.3 安装 numpy。有人可以指导我怎么做吗?我自己想不通。顺便说一句,如果有帮助,我正在使用 Ubuntu 12.04。
编辑 2: 我对此做了更多的研究。我的 /usr/lib 包含两个目录 python2.7 和 python3。而 Python2.7 目录由大量文件和子目录组成,python3 目录只有 dist-packages 子目录,其中包含deb_conf.py anf lsb_release.py ..我想几个月前我尝试了 python3 然后删除了它..但是现在 python2.7 是我唯一拥有的东西。
编辑 3:
所以这里是你让我输入的命令的输出
~$ virtualenv --no-site-package -p /usr/bin/python2.7 my_env
Running virtualenv with interpreter /usr/bin/python2.7
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in my_env/bin/python2.7
Not overwriting existing python script my_env/bin/python (you must use my_env/bin/python2.7)
Installing distribute..............................................................................................................................................................................................done.
~$ source my_env/bin/activate
~$ pip install numpy
最后一条命令生成了很多日志,这些日志以这样的结尾..
Creating build/scripts.linux-i686-2.7/f2py2.7
adding 'build/scripts.linux-i686-2.7/f2py2.7' to scripts
changing mode of build/scripts.linux-i686-2.7/f2py2.7 from 664 to 775
changing mode of /home/sandeep/my_env/bin/f2py2.7 to 775
Successfully installed numpy
Cleaning up...
在所有这些之后,我尝试再次运行 python,这是输出。
~$ python
Python 2.7.3 (default, Jan 20 2013, 21:40:19)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>
【问题讨论】:
-
您的系统上可能有多个版本的 Python。
-
相信你可以在指定版本的同时使用pip安装包。
-
是的,事实上,您可以利用 pip 将 numpy 安装到您选择的 Python 版本。更多信息:stackoverflow.com/a/10919761/42346
-
@BrianCain 输出哪个 python 是这个 /usr/local/bin/python
-
@bernie 我的系统中可能有多个版本的python。但我如何确保这一点?此外,当我在终端上运行 python 时,它会显示“Python 2.7.3(默认,2013 年 1 月 20 日,21:40:19)”
标签: python python-2.7 numpy terminal