当需要编译 Cython 时,python setup.py install 的最佳替代品是什么?

jon*_*two 25 python setuptools cython

在最新版本的setuptools中,该python setup.py install命令已被弃用(有关更多信息,请参阅https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html )。

(venv) [jon@dev02 py38]$ python setup.py install
running install
/home/jon/.jenkins/workspace/Farm_revision_linux_py36/TOXENV/py38/venv/lib64/python3.8/site-
packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is 
deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
/home/jon/.jenkins/workspace/Farm_revision_linux_py36/TOXENV/py38/venv/lib64/python3.8/site-
packages/setuptools/command/easy_install.py:156: EasyInstallDeprecationWarning: easy_install 
command is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
running bdist_egg
running egg_info
... etc
Run Code Online (Sandbox Code Playgroud)

建议您只pip install .从源代码安装包,但这不会编译任何 Cython 代码。执行此操作的最佳方法是什么?

Cython文档仍然建议使用setup.py,但我找不到任何更好的建议。看来开发人员安装(pip install -e .)确实编译了 Cython 文件,或者您可以python setup.py build_ext --inplace在运行后进行编译pip install .。这两个选项都不理想,所以我想知道是否有建议的替代方案。

编辑:

包 setup.py 文件包含以下代码,该代码应编译 Cython 文件:

try:
    import numpy
    from Cython.Build import cythonize
    cython_files = ["farm/rasters/water_fill.pyx"]
    cython_def = cythonize(cython_files, annotate=True,
                           compiler_directives={'language_level': str(sys.version_info.major)})
    include_dirs = [numpy.get_include()]
except ImportError:
    cython_def = None
    include_dirs = None
Run Code Online (Sandbox Code Playgroud)

使用 进行安装时python setup.py install,farm/rasters 目录包含以下文件:

water_fill.c
water_fill.cpython-38-x86_64-linux-gnu.so
water_fill.html
water_fill.pyx
Run Code Online (Sandbox Code Playgroud)

使用 进行安装时pip install .,该目录不包含 .so 文件,当我们尝试运行 water_fill 测试时,我们会收到如下错误

________________________________ TestFlowDown1D.test_distribute_1d_trough_partial_3 _________________________________
farm/rasters/tests/test_flood_enhance.py:241: in test_distribute_1d_trough_partial_3
    actual_arr = water_fill.water_fill_1d(arr, additional_value)
E   AttributeError: 'NoneType' object has no attribute 'water_fill_1d'

Run Code Online (Sandbox Code Playgroud)

小智 3

我相信如果您保留setup.py但添加pyproject.toml. https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html中对此进行了描述。

这是他们给出的示例pyproject.toml文件:

[build-system]
requires = ["setuptools", "wheel", "Cython"]
Run Code Online (Sandbox Code Playgroud)