为什么导入numpy和pydoc时出现“段错误”?

4 投票
4 回答
4502 浏览
提问于 2025-04-17 23:10

我安装了 numpy 这个库,并且是这样调用的。但是为什么会出现段错误(segfault)呢?我该怎么解决这个问题呢?

[pdubois@mymachine Tools]$ pip install numpy 
Requirement already satisfied (use --upgrade to upgrade): numpy in /misc/u21/pdubois/.python2.7.6/lib/python2.7/site-packages/numpy-1.9.0.dev_688b243-py2.7-linux-x86_64.egg
Cleaning up..

我也用 easy_install 安装过。

[pdubois@mymachine ~]$ easy_install-2.7.6 numpy 
Searching for numpy
Best match: numpy 1.9.0.dev-688b243
Processing numpy-1.9.0.dev_688b243-py2.7-linux-x86_64.egg
numpy 1.9.0.dev-688b243 is already the active version in easy-install.pth
Installing f2py script to /u21/pdubois/.python2.7.6/bin

Using /misc/u21/pdubois/.python2.7.6/lib/python2.7/site-packages/numpy-1.9.0.dev_688b243-py2.7-linux-x86_64.egg
Processing dependencies for numpy
Finished processing dependencies for numpy

结果给我这个错误:

[pdubois@mymachine Tools]$ python 
Python 2.7.6 (default, Feb  4 2014, 10:19:53) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy 
Segmentation fault

类似地,当我用 pydoc 调用它时:

[pdubois@mymachine Tools]$ pydoc numpy 
Segmentation fault

4 个回答

0

我在导入由GCC 4.1.2编译的numpy 1.9.0时遇到了同样的问题。这个问题通过使用GCC 4.4.7解决了。

1

你试过安装1.8版本而不是1.9版本吗?因为1.9版本还是测试版。一般来说,安装1.8版本的numpy应该是可以正常工作的。

1

在文件 pydoc.py 里面,你需要修改默认的函数:

def pipepager(text, cmd):
    """Page through text by feeding it to another program."""
    pipe = os.popen(cmd, 'w')
    try:
        pipe.write(text)
        pipe.close()
    except IOError:
        pass # Ignore broken pipes caused by quitting the pager program.

改成:

def pipepager(text, cmd):
    """Page through text by feeding it to another program."""
    import subprocess
    pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE, shell=True).stdin
    try:
        pipe.write(text)
        pipe.close()
    except IOError:
        pass # Ignore broken pipes caused by quitting the pager program.

显然,当你更改 os.popen() 的时候,问题就解决了。

想了解更多信息,可以查看 这里

3

我建议你试着用 virtualenv 创建一个 Python 虚拟环境,然后在这个环境里安装 numpy,看看问题是否还会出现。

有时候,numpy 在和系统自带的 Python 安装一起使用时会出现问题。关于这个问题,有个讨论可以在 这里 找到。你可以去看看,了解更多相关信息。

另外,numpy 依赖的一些库也可能会导致问题。有人在讨论中提到,ATLAS 库可能是个问题,如果不安装 ATLAS 来安装 numpy,可能就能解决这个问题。

撰写回答