简体   繁体   English

setup.py适用于依赖于cython和f2py的软件包

[英]setup.py for packages that depend on both cython and f2py

I would like to create a setup.py script for a python package with several submodules that depend on both cython and f2py. 我想为python包创建一个setup.py脚本,其中包含几个依赖于cython和f2py的子模块。 I have attempted to use setuptools and numpy.distutils, but have so far failed: 我试图使用setuptools和numpy.distutils,但到目前为止失败了:

Using setuptools 使用setuptools

I am able to compile my cython extensions (and create an installation for the rest of the package) using setuptools. 我可以使用setuptools编译我的cython扩展(并为包的其余部分创建一个安装)。 I have, however, been unable to figure out how to use setuptools to generate the f2py extension. 但是,我无法弄清楚如何使用setuptools来生成f2py扩展。 After extensive searching, I only found rather old messages like this one that state that f2py modules must be compiled using numpy.distutils. 经过广泛的搜索,我只发现了像这样的消息 ,声明必须使用numpy.distutils编译f2py模块。

Using numpy.distutils 使用numpy.distutils

I am able to compile my f2py extensions (and create an installation for the rest of the package) using numpy.distutils. 我可以使用numpy.distutils编译我的f2py扩展(并为包的其余部分创建安装)。 I have, however, been unable to figure out how to get numpy.distutils to compile my cython extensions as it always attempts to use pyrex to compile it (and I am using extensions specific to cython) recent. 但是,我一直无法弄清楚如何让numpy.distutils编译我的cython扩展,因为它总是尝试使用pyrex来编译它(我使用的是特定于cython的扩展)。 I have done a search to figure out how to get numpy.distutils for cython files and - at least as of a year ago - they recommend applying a monkey patch to numpy.distutils. 我已经做了一个搜索来弄清楚如何为cython文件获取numpy.distutils - 至少在一年前 - 他们建议将一个猴子补丁应用于numpy.distutils。 It seems applying such a monkey patch also restricts the options that can be passed to Cython. 似乎应用这样的猴子补丁也限制了可以传递给Cython的选项。

My question is: what is the recommended way to write a setup.py script for packages that depend on both f2py and cython? 我的问题是:为依赖于f2py和cython的软件包编写setup.py脚本的推荐方法是什么? Is applying a patch to numpy.distutils really the way to go still? 是否应用numpy.distutils补丁真的要走了吗?

You can just call each separately in your setup.py as in 您可以在setup.py中单独调用每个
http://answerpot.com/showthread.php?601643-cython%20and%20f2py 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'] )],
)

Your calling context (I think they call this namespace, not sure) 你的调用上下文(我认为他们称之为命名空间,不确定)
has to change as to what the current object Extension and function 必须改变当前对象的扩展和功能
setup() is. setup()是。

The first setup() call, it's the distutils.extension.Extension 第一个setup()调用,它是distutils.extension.Extension
and distutils.core.setup() 和distutils.core.setup()

The second setup() call, it's the numpy.distutils.core.Extension 第二个setup()调用,它是numpy.distutils.core.Extension
and numpy.distutils.core.setup() 和numpy.distutils.core.setup()

Turns out this is no longer true. 原来这不再是真的。 With both setuptools and distutils (at least the numpy version) it is possible to have extensions with C, Cython and f2py. 使用setuptoolsdistutils (至少是numpy版本),可以使用C,Cython和f2py进行扩展。 The only caveat is that to compile f2py modules one must always use numpy.distutils for both the setup and Extension functions. 唯一需要注意的是,要编译f2py模块,必须始终对setupExtension函数使用numpy.distutils But setuptools can still be used for the installation (for example, allowing the installation of a developer version with python setup.py develop ). 但是setuptools仍然可以用于安装(例如,允许使用python setup.py develop安装开发人员版本)。

To use distutils exclusively you use the following: 要专门使用distutils请使用以下命令:

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

To use setuptools , you need to import it before the distutils imports: 要使用setuptools ,您需要在distutils导入之前导入它:

import setuptools

And then the rest of the code is identical: 然后其余的代码是相同的:

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)

Obviously you need to put all your other stuff in the setup() call. 显然你需要将所有其他东西放在setup()调用中。 In the above I assume that you'll use numpy with Cython, along with an external C file ( my_ext_c_file.c ) that will be at my_c_dir/ , and that the f2py module is only one Fortran file. 在上面我假设你将使用numpy和Cython,以及将在my_c_dir/的外部C文件( my_ext_c_file.c ),并且f2py模块只是一个Fortran文件。 Adjust as needed. 根据需要调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM