Python 将Cython标记为生成依赖项?

Python 将Cython标记为生成依赖项?,python,pip,cython,distutils,pypi,Python,Pip,Cython,Distutils,Pypi,有一个带有setup.py的Python包,其内容如下: from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext setup( name = 'fastahack', ext_modules=[ Extension("fastahack.cfastahack", sources=[

有一个带有setup.py的Python包,其内容如下:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
  name = 'fastahack',
  ext_modules=[
    Extension("fastahack.cfastahack",
              sources=["fastahack/cfastahack.pyx", "lib/Fasta.cpp", "lib/split.cpp"],
              libraries=["stdc++"],
              include_dirs=["lib/"],
              language="c++"),
    ],
    package_data = {'lib': ['*.pyx', "*.c", "*.h", "README.rst"]},
    package_dir = {"fastahack": "fastahack"},
    cmdclass = {'build_ext': build_ext},
    packages = ['fastahack', 'fastahack.tests'],
    author = "Brent Pedersen",
    author_email="bpederse@gmail.com",
    #test_suite='nose.collector'
)
如果未安装Cython,则无法导入此setup.py。据我所知,导入setup.py是像pip这样的工具计算包的依赖关系的方式。我想设置这个软件包,这样它就可以上传到PyPI,事实上它取决于Cython,所以当您尝试“pip安装fastahack”时,或者当您尝试直接从Git存储库“pip安装”时,Cython将被下载并安装


我如何打包此模块,以便在未安装Cython的情况下从Internet正确安装?始终使用最新版本的Cython会更好。

对于
Cython
导入,请使用
尝试和
除外,并根据导入是否成功修改
设置。查看熊猫的列表,了解setup.py的标准模板:

have_cython = False try: from Cython.Distutils import build_ext as _build_ext have_cython = True except ImportError: from distutils.command.build_ext import build_ext as _build_ext if have_cython: foo = Extension('foo', ['src/foo.pyx']) else: foo = Extension('foo', ['src/foo.c']) setup ( ... ext_modules=[foo], cmdclass={'build_ext': build_ext} have_cython=False 尝试: 从Cython.Distutils导入build\u ext作为\u build\u ext have_cython=True 除恐怖外: 从distutils.command.build\u ext导入build\u ext as\u build\u ext 如果有_cython: foo=扩展名('foo',['src/foo.pyx']) 其他: foo=扩展名('foo',['src/foo.c']) 设置( ... ext_模块=[foo], cmdclass={'build\u ext':build\u ext}
别忘了在软件包中提供extention.c文件,这将允许用户在不安装cython的情况下构建模块。

您可以使用项目规范将cython指定为构建依赖项

在文件
pyproject.toml
(与
setup.py
位于同一目录)中插入:

Cython将在构建包之前安装


请注意,(当前)如果您在本地安装的软件包是可编辑的(即使用
--editable
-e
),则需要将
--no-use-pep517
传递到
pip install

这并不是我们想要的;目标是自动安装cython作为依赖项…@SamB,你有没有关于将cython作为依赖项的更新?@Zyzue:没有,我也没有找到方法。
[build-system]
requires = ["setuptools", "wheel", "Cython"]