Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在涉及Cython的setup.py中,如果install_需要,那么如何从库导入内容?_Python_Pip_Cython_Setup.py - Fatal编程技术网

Python 在涉及Cython的setup.py中,如果install_需要,那么如何从库导入内容?

Python 在涉及Cython的setup.py中,如果install_需要,那么如何从库导入内容?,python,pip,cython,setup.py,Python,Pip,Cython,Setup.py,这对我来说毫无意义。如何使用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', 'my

这对我来说毫无意义。如何使用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构建

Traceback (most recent call last):
  File "setup.py", line 3, in <module>
    from Cython.Build import cythonize
ImportError: No module named Cython.Build
回溯(最近一次呼叫最后一次):
文件“setup.py”,第3行,在
从Cython.Build导入cythonize
ImportError:没有名为Cython.Build的模块
因为cython还没有安装


奇怪的是,很多项目都是这样写的。快速的github搜索会发现很多信息:

一般来说没有意义。正如您所怀疑的,这是一种试图使用(可能)尚未安装的东西的行为。如果在已经安装了依赖项的系统上进行测试,您可能不会注意到此缺陷。但是在没有依赖关系的系统上运行它,您肯定会注意到

还有另一个
setup()
关键字参数,
setup\u requires
,它在形式上看起来是并行的,用于
install\u requires
,但这是一种错觉。尽管
install\u requires
在缺乏它所命名的依赖关系的环境中触发了一系列可爱的自动安装,但是
setup\u requires
更多的是文档而不是自动化。它不会自动安装,当然也不会神奇地及时跳回自动安装已在
import
语句中调用的模块

这里有更多的内容,但快速的回答是,您被一个试图自动安装自己的安装先决条件的模块所迷惑是对的


对于实际解决方法,请尝试分别安装
cython
,然后运行此安装程序。虽然它不会修复这个设置脚本中形而上学的幻觉,但它会解决需求,让您继续前进

一种解决方案是不将Cython作为构建需求,而是将Cython生成的
C
文件与包一起分发。我确信在某个地方有一个更简单的例子,但这就是
pandas
所做的-它有条件地导入Cython,如果不存在,可以从c文件构建

编辑:来自@danny的文档链接有一个更容易遵循的示例。

据我所知,这就是它的用武之地——它的一位作者也看到了这一点

我们的想法是向Python项目/包中添加另一个文件:
pyproject.toml
。它应该包含关于构建环境依赖性的信息(包括长期的)
pip(或任何其他软件包管理器)可以查看此文件,并在运行setup.py(或任何其他构建脚本)之前安装所需的构建环境。因此,
pyproject.toml
可以如下所示:

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

这是一个相当新的开发,到目前为止(2019年1月),它还没有得到Python社区的最终确定/批准,尽管在2017年5月/10.0版本中(有限)。

当您使用setuptool时,您应该将
cython
添加到
setup\u requires
(如果安装使用cython,还应添加到
install\u requires
),即

不会导入
Cython
(当
setup.py
启动时,它还不可用),但会使用
setuptools.Extension
而不是
cythonize
将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扩展使用
numpy
,它会变得更复杂,但这也是可能的-请参阅。

奇怪的是,很多项目都使用我编写的代码。在GitHub上快速搜索可以发现大量的信息。没有人在新安装上测试他们的代码吗?这样做不太方便。大型支持包(例如,
cython
lxml
pandas
)通常会成为潜在的期望。许多测试都是在Travis CI上进行的,或者在本地使用虚拟环境、
tox
等进行的。但是这些构建环境获得了定制的安装过程,这些过程并不存在,只是从头开始手动运行安装程序。很容易就有出色的运行测试——在十几个不同的Python版本中自动构建,覆盖率为100%——这些测试不能单独从
setup.py
从头开始正确安装。我去过那里,做过那件事。尴尬,烦人,太容易了。这是最好的答案和答案。通常,pyx文件不需要在安装时重新评估,只有在其源代码发生更改时才需要重新评估。因此,对于可分发软件包,只有Cython生成的C/C++文件需要由setup.py分发和构建。
...
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
...