【问题标题】:Compiling and distributing Cython extensions编译和分发 Cython 扩展
【发布时间】:2015-01-09 19:01:57
【问题描述】:

我有一个 python 包(python 2.7),其中包含一些用 cython 编写的优化函数。我的 setup.py 处理纯 python 文件,安装的包使用未优化的纯 python 函数。

我正在尝试了解设置项目以分发 C 扩展的最佳方式。

现在我在扩展文件夹中有一个特定的setup.py,可用于编译扩展:

python setup.py build_ext --inplace

但我希望软件包安装也能处理 C 扩展。可能只有一个:

python setup.py

我正在考虑将特定的 setup.py 合并到项目范围的 setup.py 中。但是,要编译扩展,我需要导入 numpy.但我知道不鼓励在 setup.py 中导入标准库之外的任何内容。

python 包的最佳实践是什么,包括仅使用 numpy 而没有其他外部库的简单 cython 扩展?

【问题讨论】:

标签: python numpy cython packaging setuptools


【解决方案1】:

现在我选择了以下需要导入 numpy.到目前为止还没有造成问题。

setup.py

from setuptools import setup
from setuptools.extension import Extension
import numpy as np
import versioneer

## Metadata
project_name = 'foobar'
long_description = """
Long description in RST. Used by PyPI.
"""

## Configure versioneer
versioneer.VCS = 'git'
versioneer.versionfile_source = project_name + '/_version.py'
versioneer.versionfile_build = project_name + '/_version.py'
versioneer.tag_prefix = '' # tags are like 1.2.0
versioneer.parentdir_prefix = project_name + '-'

## Configuration to build Cython extensions
try:
    from Cython.Distutils import build_ext
except ImportError:
    # cython is not installed, use the .c file
    has_cython = False
    ext_extention = '.c'
else:
    # cython is installed, use .pyx file
    has_cython = True
    ext_extention = '.pyx'
ext_modules = [Extension("corecalculation_c",
                         [project_name + \
                         "/calculation/corecalculation_c" + ext_extention])]

## Configure setup.py commands
cmdclass = versioneer.get_cmdclass()
if has_cython:
    cmdclass.update(build_ext=build_ext)


setup(name = project_name,
      version = versioneer.get_version(),
      cmdclass = cmdclass,
      include_dirs = [np.get_include()],
      ext_modules = ext_modules,
      author = 'Author Name',
      author_email = 'email@address',
      url          = 'http://github.com/USER/PROJECT/',
      download_url = 'http://github.com/USER/PROJECT/',
      install_requires = ['numpy', 'scipy', 'matplotlib', 'ipython'],
      license = 'GPLv2',
      description = ("Oneline description"),
      long_description = long_description,
      platforms = ('Windows', 'Linux', 'Mac OS X'),
      classifiers=['Intended Audience :: Science/Research',
                   'Operating System :: OS Independent',
                   'Programming Language :: Python',
                   'Programming Language :: Python :: 2.7',
                   'Topic :: Scientific/Engineering',
                   ],
      packages = [project_name, project_name+'.utils'],
      keywords = 'keyword1 keyword2',
      )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2013-02-07
    • 2013-01-29
    • 2020-07-18
    • 1970-01-01
    相关资源
    最近更新 更多