簡體   English   中英

在涉及 Cython 的 setup.py 中,如果 install_requires,那么如何從庫中導入一些東西?

[英]In a setup.py involving Cython, if install_requires, then how can from library import something?

這對我來說沒有意義。 如何使用 setup.py 安裝 Cython,然后還使用 setup.py 編譯庫代理?

import sys, imp, os, glob
from setuptools import setup
from Cython.Build import cythonize # this isn't installed yet

setup(
    name='mylib',
    version='1.0',
    package_dir={'mylib': 'mylib', 'mylib.tests': 'tests'},
    packages=['mylib', 'mylib.tests'],
    ext_modules = cythonize("mylib_proxy.pyx"), #how can we call cythonize here?
    install_requires=['cython'],
    test_suite='tests',
)

后來:python setup.py build

Traceback (most recent call last):
  File "setup.py", line 3, in <module>
    from Cython.Build import cythonize
ImportError: No module named Cython.Build

這是因為尚未安裝 cython。

奇怪的是,很多項目都是這樣編寫的。 一個快速的 github 搜索揭示了: https : //github.com/search? utf8 =% E2%9C%93&q=install_requires+cython&type =Code

據我了解,這就是PEP 518 的用武之地 - 另請參閱其作者之一的一些說明

這個想法是您將另一個文件添加到您的 Python 項目/包: pyproject.toml 它應該包含有關構建環境依賴項的信息(以及其他內容,長期)。 pip (或任何其他包管理器)可以查看此文件,並在運行 setup.py (或任何其他構建腳本)之前安裝所需的構建環境。 因此, pyproject.toml可能如下所示:

[build-system]
requires = ["setuptools", "wheel", "Cython"]

這是一個相當新的開發,到目前為止(2019 年 1 月),它還沒有最終確定/得到 Python 社區的批准,盡管(有限)支持已在 2017 年 5 月/10.0 版本添加到 pip

對此的一種解決方案是不將 Cython 設為構建要求,而是將 Cython 生成的C文件與您的包一起分發。 我確定某處有一個更簡單的例子,但這就是pandas所做的 - 它有條件地導入 Cython,如果不存在,可以從 c 文件構建。

https://github.com/pandas-dev/pandas/blob/3ff845b4e81d4dde403c29908f5a9bbfe4a87788/setup.py#L433

編輯:@danny 的文檔鏈接有一個更容易理解的例子。 http://docs.cython.org/en/latest/src/reference/compilation.html#distributing-cython-modules

當您使用 setuptool 時,您應該將cython添加到setup_requires (如果安裝時使用了 cython,也應該添加到install_requires ),即

# don't import cython, it isn't yet there
from setuptools import setup, Extension

# use Extension, rather than cythonize (it is not yet available)
cy_extension = Extension(name="mylib_proxy", sources=["mylib_proxy.pyx"])

setup(
    name='mylib',
    ...
    ext_modules = [cy_extension],
    setup_requires=["cython"],
    ...
)

Cython未導入(在setup.py啟動setuptools.Extension用),但使用setuptools.Extension代替cythonize將 cython-extension 添加到設置中。

它現在應該可以工作了。 原因: setuptoolssetup_requires滿足后嘗試導入 cython

...
try:
    # Attempt to use Cython for building extensions, if available
    from Cython.Distutils.build_ext import build_ext as _build_ext
    # Additionally, assert that the compiler module will load
    # also. Ref #1229.
    __import__('Cython.Compiler.Main')
except ImportError:
    _build_ext = _du_build_ext
...

如果您的 Cython-extension 使用numpy ,它會變得更加復雜,但這也是可能的 - 請參閱此 SO post

一般來說沒有意義。 正如您懷疑的那樣,這是嘗試使用(可能)尚未安裝的東西。 如果在已安裝依賴項的系統上進行測試,您可能不會注意到此缺陷。 但是在沒有您的依賴項的系統上運行它,您肯定會注意到。

還有另一個setup()關鍵字參數setup_requires ,它在形式上似乎與install_requires平行,但這是一種錯覺。 install_requires在缺乏它所命名的依賴項的環境中觸發了自動安裝的可setup_requires ,而setup_requires是更多的文檔而不是自動化。 它不會自動安裝,當然也不會神奇地及時跳回到自動安裝已經在import語句中調用的模塊。

setuptools docs 中有更多關於此的內容,但快速回答是您對試圖自動安裝自己的安裝先決條件的模塊感到困惑是正確的。

對於實際的解決方法,請嘗試單獨安裝cython ,然后運行此設置。 雖然它不會解決此設置腳本的形而上學幻覺,但它會解決要求並讓您繼續前進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM