在setup.py中安装numpy和pandas作为依赖

9 投票
2 回答
5274 浏览
提问于 2025-04-20 18:08

通过setuptools在setup.py中作为依赖项安装numpypandas对我来说不太管用。这不是因为缺少依赖项。如果我先用pip install numpy安装了numpy,然后再运行python setup.py develop,一切都能正常工作。如果我没理解错setuptools的文档,所有的包都是先构建再安装的。所以在构建pandas时,numpy虽然被构建了,但并没有被安装。

作为一个解决办法,我把numpy加到了我的setup_requires中。这样可以正常工作,但显然不是一个很干净的解决方案。

有没有人知道一个干净的解决方案(只限Linux)来通过setuptools安装numpy和pandas?

更新:

依赖项是通过以下方式配置的:

install_requires=['numpy','pandas']

无论我是否明确添加numpy,结果都没有区别。在这两种情况下,numpy都会被下载和构建,但pandas构建失败,因为找不到一些头文件(这些头文件可能是在安装numpy时安装的,但在构建时没有)。如果我先安装numpy,一切就能正常工作。我可以100%复现这个问题,而且与我正在做的项目无关。

更新2:

这是堆栈跟踪的最后部分:

  File "/tmp/easy_install-QMa4ce/pandas-0.14.1/temp/easy_install-f6lreI/numpy-1.9.0/numpy/distutils/command/build_src.py", line 153, in run
  File "/tmp/easy_install-QMa4ce/pandas-0.14.1/temp/easy_install-f6lreI/numpy-1.9.0/numpy/distutils/command/build_src.py", line 170, in build_sources
  File "/tmp/easy_install-QMa4ce/pandas-0.14.1/temp/easy_install-f6lreI/numpy-1.9.0/numpy/distutils/command/build_src.py", line 329, in build_extension_sources
  File "/tmp/easy_install-QMa4ce/pandas-0.14.1/temp/easy_install-f6lreI/numpy-1.9.0/numpy/distutils/command/build_src.py", line 386, in generate_sources
  File "numpy/core/setup.py", line 432, in generate_config_h

  File "numpy/core/setup.py", line 42, in check_types
    entry_points={
  File "numpy/core/setup.py", line 293, in check_types

SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.

最后的消息肯定是错误的。如果我在运行python setup.py develop之前先执行pip install numpy,一切都能正常工作。在上面的例子中,我的install_requires中只有pandas,没有numpy。但据我所知,添加不添加numpy都没有区别。

2 个回答

2

这些依赖项应该在设置时通过 install_requires 这个参数来声明。这里有一个例子,来自一个叫做 geopandas 的项目,它需要 pandas:查看示例

setup(name='geopandas',
      version=FULLVERSION,
      description='Geographic pandas extensions',
      license='BSD',
      author='Kelsey Jordahl',
      author_email='kjordahl@enthought.com',
      url='http://geopandas.org',
      long_description=LONG_DESCRIPTION,
      packages=['geopandas', 'geopandas.io', 'geopandas.tools'],
      install_requires=[
        'pandas', 'shapely', 'fiona', 'descartes', 'pyproj', 'rtree'],  # here
)

你还可以指定需要的版本,具体可以参考 setuptools 的文档。因为你通常希望确保使用的版本是最新的(这样才能有你需要的功能和修复的bug)——这里有一个例子,展示我在 pep8radius 中是如何做到的:查看示例

7

请查看这个未解决的问题 https://github.com/numpy/numpy/issues/2434

这是一个在numpy中已知的错误,和setuptools有关。

在讨论中提到,使用 $ pip install -e .$ python setup.py develop 更好——效果是一样的,但可以避免这个问题。

撰写回答