编译和分发Cython扩展程序

3

我有一个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中导入标准库之外的任何内容都是不被鼓励的。

对于包含简单的cython扩展模块且仅使用numpy而没有其他外部库的Python包,最佳实践是什么?


但我知道在setup.py中导入标准库之外的任何内容都是不被鼓励的。你从哪里找到这个信息的? - cel
在新的虚拟环境中安装软件包的依赖项时,它会中断安装过程。例如,请参见:https://github.com/lmfit/lmfit-py/issues/149 - user2304916
我建议看看其他项目是如何解决这个问题的,例如:https://github.com/statsmodels/statsmodels/blob/master/setup.py我不会说导入第三方模块是不鼓励的。然而,事实上你不能只写 import numpy 而不遇到麻烦。使用 try 语句进行防御性导入和使用 setuptools 的 install_requires 可能会为您解决问题。 - cel
1
你知道requiresinstall_requires之间的区别吗? - user2304916
请参见以下链接:https://dev59.com/emMm5IYBdhLWcg3wTtfi - cel
完美的答案,谢谢! - user2304916
1个回答

2

目前,我采用了以下解决方案,需要导入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',
      )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接