python - numpy 无法通过终端运行

标签 python python-2.7 numpy terminal

我通过执行在 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
>>> 

最佳答案

万一什么都不起作用。

  1. 如果尚未完成,请安装 python-virtualenv。
  2. 创建虚拟环境

    virtualenv name

  3. 启动虚拟环境

    source name/bin/activate

  4. 使用easy_installpip安装numpy

  5. 利润

注意:

每次都必须激活 Virtualenv。但是您可以使用 virtualenv 包装器使该任务变得更容易。

http://virtualenvwrapper.readthedocs.org/en/latest/

使用 virtualenv 而不是 ubuntu 软件包有很多原因。在某种程度上,我建议不要尽可能多地接触“操作系统”python。如果您的项目需要它,请使用 virtualenv。 virtualenv 中的 Python 不会与其他应用程序发生冲突,并且您不必使用 sudo 来安装新包。

关于python - numpy 无法通过终端运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15838252/

相关文章:

python - 如何在 Python 中读取 Unicode 输入并比较 Unicode 字符串?

python - 使用 Pillow/numpy 在任何分割带上执行 .dot 后,无法合并分割图像

numpy - 如何计算图像中的 RGB 或 HSV channel 组合?

python - 更改 numpy 中矩阵的顺序

python - 在 python 中保存 `.npz` 文件而不是 `.npy` 在速度、内存和查找方面有什么优势?

Python MQTT 连接到 Azure Iot Hub

python - 无法使用 matplotlib.pyplot 在 Pycharm 中绘图。图形出现并立即消失

python - Selenium:在谷歌浏览器中上传文件

python - Django 在修改 less 文件时运行 bash 脚本

python - Django 1.7 makemigrations - ValueError : Cannot serialize class