如何在Python shell中导入NumPy

18

我尝试在Python中导入NumPy,但没有成功:

>>> import numpy as np
    x=np.array([[7,8,5],[3,5,7]],np.int32)

   Traceback (most recent call last):
   File "<pyshell#3>", line 1, in <module>
   import numpy as np
   File "C:\Python27\lib\numpy\__init__.py", line 127, 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.

我该如何修复这个问题?

2个回答

20

这个信息已经很清楚了:当你调用Python时,你的工作目录不应该是NumPy源目录;NumPy应该被安装,而你的工作目录应该是它所在的目录之外的任何地方。


如何更改我的工作目录。我知道在Matlab中如何做,但我是Python的新用户,不知道该怎么做。 - hafizul asad
1
在终端中,在启动Python之前,请确保您在其他目录中。只要numpy文件夹位于系统的PYTHONPATH变量的一部分,您就可以从系统上的任何位置在Python中导入numpy。如果您不确定所有这些含义,请搜索PYTHONPATH和Python导入。 - DaveTheScientist
我尝试了这个,但没有成功; ,>>> os.getcwd() 'C:\Python27'
os.chdir('C:\working directory') import numpy
- hafizul asad
返回翻译后的文本:cd ../.. 然后打开 Python shell。它说你不能从 C:\Python27\lib\numpy\ 中导入 numpy - Ryan Haining

1

Debian/Ubuntu上:

aptitude install python-numpy

在Windows系统上,下载安装程序:
http://sourceforge.net/projects/numpy/files/NumPy/

在其他系统上,下载tar.gz文件并运行以下命令:
$ tar xfz numpy-n.m.tar.gz
$ cd numpy-n.m
$ python setup.py install

2
他不需要安装NumPy。 - user4805123

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