setup.py 用于同时依赖 cython 和 f2py 的包

发布于 2024-12-12 14:55:26 字数 826 浏览 3 评论 0原文

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

使用 setuptools,

我可以使用 setuptools 编译我的 cython 扩展(并为包的其余部分创建安装)。但是,我无法弄清楚如何使用 setuptools 生成 f2py 扩展。经过广泛的搜索,我只发现了相当旧的像这样的消息,其中指出f2py 模块必须使用 numpy.distutils 进行编译。

使用 numpy.distutils

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

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

I would like to create a setup.py script for a python package with several submodules that depend on both cython and f2py. I have attempted to use setuptools and numpy.distutils, but have so far failed:

Using setuptools

I am able to compile my cython extensions (and create an installation for the rest of the package) using setuptools. I have, however, been unable to figure out how to use setuptools to generate the f2py extension. After extensive searching, I only found rather old messages like this one that state that f2py modules must be compiled using numpy.distutils.

Using numpy.distutils

I am able to compile my f2py extensions (and create an installation for the rest of the package) using numpy.distutils. 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. 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. It seems applying such a monkey patch also restricts the options that can be passed to Cython.

My question is: what is the recommended way to write a setup.py script for packages that depend on both f2py and cython? Is applying a patch to numpy.distutils really the way to go still?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下壹個目標 2024-12-19 14:55:26

您可以在 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() 是。

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

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

You can just call each separately in your setup.py as in
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.

The first setup() call, it's the distutils.extension.Extension
and distutils.core.setup()

The second setup() call, it's the numpy.distutils.core.Extension
and numpy.distutils.core.setup()

_蜘蛛 2024-12-19 14:55:26

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

要专门使用 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() 调用中。在上面,我假设您将 numpy 与 Cython 一起使用,以及位于 my_c_dir/ 的外部 C 文件 (my_ext_c_file.c),并且 < code>f2py 模块只是一个 Fortran 文件。根据需要进行调整。

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. The only caveat is that to compile f2py modules one must always use numpy.distutils for both the setup and Extension functions. But setuptools can still be used for the installation (for example, allowing the installation of a developer version with python setup.py develop).

To use distutils exclusively you use the following:

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

To use setuptools, you need to import it before the distutils imports:

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. 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. Adjust as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文