使用setuptools安装numpy依赖项

23
这可能是这个问题的后续问题。
我正在使用setuptools来安装我的一个软件包。作为依赖项,我列出了numpy。我正在使用Python2.7,并且当我执行以下操作时:
python setup.py install

有这样一个 setup.py 文件:

from setuptools import setup

setup(name = "test_pack", install_requires = ["numpy"])

我最终得到了这个错误信息:

ImportError: No module named numpy.distutils

为了将numpy作为依赖项并在不安装python-dev的情况下安装它,我需要做什么?


python setup.py install的完整输出:

running install
running bdist_egg
running egg_info
writing requirements to test_pack.egg-info/requires.txt
writing test_pack.egg-info/PKG-INFO
writing top-level names to test_pack.egg-info/top_level.txt
writing dependency_links to test_pack.egg-info/dependency_links.txt
reading manifest file 'test_pack.egg-info/SOURCES.txt'
writing manifest file 'test_pack.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/test_pack
copying build/lib/test_pack/__init__.py -> build/bdist.linux-x86_64/egg/test_pack
copying build/lib/test_pack/mod.py -> build/bdist.linux-x86_64/egg/test_pack
byte-compiling build/bdist.linux-x86_64/egg/test_pack/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/test_pack/mod.py to mod.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying test_pack.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying test_pack.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying test_pack.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying test_pack.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying test_pack.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating 'dist/test_pack-0.0.0-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing test_pack-0.0.0-py2.7.egg
Copying test_pack-0.0.0-py2.7.egg to /home/woltan/local/lib/python2.7/site-packages
Adding test-pack 0.0.0 to easy-install.pth file

Installed /home/woltan/local/lib/python2.7/site-packages/test_pack-0.0.0-py2.7.egg
Processing dependencies for test-pack==0.0.0
Searching for numpy
Reading http://pypi.python.org/simple/numpy/
Reading http://numpy.scipy.org
Reading http://sourceforge.net/project/showfiles.php?group_id=1369&package_id=175103
Reading http://numeric.scipy.org
Best match: numpy 1.6.1
Downloading http://pypi.python.org/packages/source/n/numpy/numpy-1.6.1.zip#md5=462c22b8eb221c78ddd51de98fbb5979
Processing numpy-1.6.1.zip
Running numpy-1.6.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-AoFmdV/numpy-1.6.1/egg-dist-tmp-JH1j2R
non-existing path in 'numpy/distutils': 'site.cfg'
Could not locate executable g77
Found executable /opt/solstudio12.2/bin/f77
gnu: no Fortran 90 compiler found
gnu: no Fortran 90 compiler found
Found executable /opt/intel/Compiler/11.1/073/bin/intel64/ifort
Could not locate executable lf95
Could not locate executable pgf90
Could not locate executable pgf77
Found executable /opt/solstudio12.2/bin/f90
Found executable /opt/solstudio12.2/bin/f95
Could not locate executable fort
_configtest.c:1: warning: conflicting types for built-in function ‘exp’
_configtest.o: In function `main':
/tmp/easy_install-AoFmdV/numpy-1.6.1/_configtest.c:6: undefined reference to `exp'
collect2: ld returned 1 exit status
_configtest.c:1: warning: conflicting types for built-in function ‘exp’
_configtest.c:1:20: error: Python.h: No such file or directory
_configtest.o: In function `main':
/tmp/easy_install-AoFmdV/numpy-1.6.1/_configtest.c:6: undefined reference to `exp'
collect2: ld returned 1 exit status
_configtest.c:1:20: error: Python.h: No such file or directory
3个回答

6
这是一个已知的问题,可以在numpy / numpy#2434 上跟踪。

我找到了一种解决方法:将numpy添加到setup_requires中。在setuptools的最新版本中,在setup_requiresinstall_requires中都有它似乎可以正常工作。

因此,您的setup.py应该如下所示:

setup(
    # Your setup specific stuff here
    setup_requires=["numpy"],  # Just numpy here
    install_requires=["numpy"],  # Add any of your other dependencies here
)

将numpy添加到setup_requires并不足够。我还必须运行python setup.py unstall两次才能成功安装numpy,详见https://dev59.com/uGEi5IYBdhLWcg3wIJPc - asmaier

3

如果您没有numpy的二进制分发(预编译/构建),则必须具有Python头文件,因为它需要这些文件来构建numpy。这就是为什么大多数软件包管理器都带有这些软件包的预编译版本的原因。例如,您可以apt-get install python-numpy,将其链接到您的虚拟环境中,并在尝试使用install_requires = ['numpy']安装程序时,它应该会看到已经安装了numpy。


1
有没有办法告诉 setuptools 在安装之前先构建 numpy?单独构建和安装 numpy 完全没有问题... - Woltan
抱歉,我不太明白。根据您所粘贴的输出,它确实正在尝试构建numpy。 - Michael Merickel
没错,但我可以自己下载并构建安装numpy(不使用setuptools)。只有当我尝试使用setuptools构建和安装它时,它才会出现问题。 - Woltan
当你自己构建它时,是否向它传递了特殊参数?我相信setuptools只是尝试运行 python setup.py install - Michael Merickel
您的标准环境与setuptools安装包时使用的环境似乎存在差异。例如,不仅找不到Python头文件,也找不到Fortran编译器(由于某些Numpy算法是用Fortran编写的,因此需要)。 - holdenweb
显示剩余2条评论

2
为了安装 numpy,setuptools 将下载该软件包并从源代码编译。但是,有一些先决条件需要编译 numpy,你可以在此处检查:这里
_configtest.c:1:20: error: Python.h: No such file or directory

这个错误提示表明,至少你没有安装Python-dev包(如果你使用的是Ubuntu / Debian)。


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