【问题标题】:setup.py for packages that depend on both cython and f2pysetup.py 用于依赖于 cython 和 f2py 的包
【发布时间】:2011-10-28 16:24:56
【问题描述】:

我想为一个 python 包创建一个 setup.py 脚本,其中包含几个依赖于 cython 和 f2py 的子模块。我曾尝试使用 setuptools 和 numpy.distutils,但到目前为止都失败了:

使用设置工具

我能够使用 setuptools 编译我的 cython 扩展(并为包的其余部分创建安装)。但是,我一直无法弄清楚如何使用 setuptools 来生成 f2py 扩展。经过大量搜索,我只找到了相当古老的messages like this one,指出必须使用 numpy.distutils 编译 f2py 模块。

使用 numpy.distutils

我能够使用 numpy.distutils 编译我的 f2py 扩展(并为包的其余部分创建安装)。但是,我一直无法弄清楚如何让 numpy.distutils 编译我的 cython 扩展,因为它最近总是尝试使用 pyrex 来编译它(而且我正在使用特定于 cython 的扩展)。我已经进行了搜索以弄清楚如何为 cython 文件获取 numpy.distutils,并且 - 至少在一年前 - 他们建议将 monkey patch 应用于 numpy.distutils。似乎应用这样的猴子补丁也限制了可以传递给 Cython 的选项。

我的问题是:为依赖于 f2py 和 cython 的包编写 setup.py 脚本的推荐方法是什么?对 numpy.distutils 应用补丁真的是可行的方法吗?

【问题讨论】:

    标签: python numpy cython f2py


    【解决方案1】:

    您可以在 setup.py 中单独调用每个,如
    http://answerpot.com/showthread.php?601643-cython%20and%20f2py

    # Cython extension
    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Distutils import build_ext
    
    setup(
      ext_modules = [Extension( 'cext', ['cext.pyx'] )],
      cmdclass = {'build_ext': build_ext},
      script_args = ['build_ext', '--inplace'],
    )
    
    # Fortran extension
    from numpy.distutils.core import setup, Extension
    setup(
      ext_modules = [Extension( 'fext', ['fext.f90'] )],
    )
    

    您的调用上下文(我认为他们调用此命名空间,不确定)
    必须改变当前对象的扩展和功能
    设置()是。

    第一个 setup() 调用,它是 distutils.extension.Extension
    和 distutils.core.setup()

    第二个 setup() 调用,它是 numpy.distutils.core.Extension
    和 numpy.distutils.core.setup()

    【讨论】:

    • 不幸的是,answerpot.com 链接不再有效。不过谢谢你的回答! :-)
    【解决方案2】:

    事实证明这不再正确。对于setuptoolsdistutils(至少是numpy 版本),可以使用C、Cython 和f2py 进行扩展。唯一需要注意的是,要编译 f2py 模块,必须始终将 numpy.distutils 用于 setupExtension 函数。但是setuptools仍然可以用于安装(例如,允许安装带有python setup.py develop的开发者版本)。

    要独占使用distutils,请使用以下内容:

    from numpy.distutils.core import setup
    from numpy.distutils.extension import Extension
    

    要使用setuptools,您需要在distutils 导入之前导入它:

    import setuptools
    

    然后剩下的代码都是一样的:

    from numpy import get_include
    from Cython.Build import cythonize
    
    NAME = 'my_package'
    NUMPY_INC = get_include()
    extensions = [
        Extension(name=NAME + ".my_cython_ext", 
                  include_dirs=[NUMPY_INC, "my_c_dir"]
                  sources=["my_cython_ext.pyx", "my_c_dir/my_ext_c_file.c"]),
        Extension(name=NAME + ".my_f2py_ext", 
                  sources=["my_f2py_ext.f"]),
    ]
    extensions = cythonize(extensions)
    setup(..., ext_modules=extensions)
    

    显然,您需要将所有其他内容放入 setup() 调用中。在上面,我假设您将在 Cython 中使用 numpy,以及一个位于 my_c_dir/ 的外部 C 文件 (my_ext_c_file.c),并且 f2py 模块只是一个 Fortran 文件。根据需要进行调整。

    【讨论】:

    • 我也有类似的问题。然而,在运行sudo python3 setup.py install 之后,我得到了error : unknown file type '.f95' (from 'resource_dependency.f95')。你能指导我吗?
    猜你喜欢
    • 2016-10-18
    • 2016-11-14
    • 2014-11-03
    • 2018-04-13
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 2010-11-24
    • 2017-04-26
    相关资源
    最近更新 更多