首页 > 解决方案 > python安装文件中的numpy依赖项

问题描述

这是一个出现的问题,但我还没有找到一个好的解决方案。所以,我有一个 python 项目,我希望用户能够将它安装在一个最小的 anaconda 虚拟环境中。

我的代码还使用 Cython 来加速一些计算密集型函数,并要求 numpy 标头能够对它们进行 cythonize。所以,我有一个设置python脚本如下:

from distutils.extension import Extension
from setuptools import setup, Extension


def create_extension():
    import numpy as np  # No luck even though I keep it at function scope
    return Extension("speed",
                     sources=["app/perf/booster.pyx"],
                     include_dirs=[np.get_include()],  # problematic
                     language="c++",
                     libraries=[],
                     extra_link_args=[])

setup(
    setup_requires=[
        'setuptools>=18.0',
        'numpy==1.13.3',
        'cython==0.28.2'
    ],

    ext_modules=[
        create_extension(),
    ],

......
install_requires=[
    'numpy==1.13.3',
    'Cython==0.28.2',
    'nibabel==2.2.1',
    'scipy==1.0.0'
],
)

但是,当我运行buildorinstall时,numpy inport 失败。虽然我已经将它添加到setup_requires组和install_requires组中,但它似乎没有任何效果。

当然,我可以要求用户预先安装 numpy,但我宁愿让它一步到位,我想知道是否有办法实现这一点。

发表评论后,我尝试了该线程中的解决方案,如下所示:

F

rom distutils.extension import Extension
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools import setup, Extension

class build_ext(_build_ext):
    def finalize_options(self):
        _build_ext.finalize_options(self)
        # Prevent numpy from thinking it is still in its setup process:
        __builtins__.__NUMPY_SETUP__ = False
        import numpy
        self.include_dirs.append(numpy.get_include())

def create_extension():
    #import numpy as np
    return Extension("speed",
                     sources=["app/perf/booster.pyx"],
                     #include_dirs=[np.get_include()],
                     language="c++",
                     libraries=[],
                     extra_link_args=[])


setup(
    cmdclass={'build_ext': build_ext},
     .....

现在这失败并出现错误:

error: unknown file type '.pyx' (from 'app/perf/booster.pyx')

标签: pythonnumpydistutilssetup.pycythonize

解决方案


推荐阅读