【问题标题】:How to import NumPy in the Python shell如何在 Python shell 中导入 NumPy
【发布时间】:2018-06-27 06:43:32
【问题描述】:

我曾尝试在 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.

我该如何解决这个问题?

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    该消息是不言自明的;调用 Python 时,工作目录不应是 NumPy 源目录;应该安装 NumPy,并且您的工作目录应该是它所在的目录以外的任何目录。

    【讨论】:

    • 如何更改我的工作目录。我知道在matlab中怎么做,但是我是python的新用户,不知道怎么做。
    • 在您的终端中,在启动 python 之前,请确保您位于其他目录中。只要 numpy 文件夹位于系统 PYTHONPATH 变量的一部分,您就可以从系统上的任何位置在 python 中导入 numpy。如果你不确定所有这些意味着什么,谷歌 PYTHONPATH 并在 python 中导入。
    • 我试过了,但没有用; ,>>> os.getcwd() 'C:\\Python27' >>> os.chdir('C:\工作目录') >>> 导入numpy
    • cd ../.. 然后打开 python shell。意思是你不能从 `C:\Python27\lib\numpy` 中导入 numpy
    【解决方案2】:

    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
    

    【讨论】:

    • 他不需要安装 NumPy。
    猜你喜欢
    • 1970-01-01
    • 2016-07-06
    • 2014-06-09
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多