use*_*424 31 python bootstrapping numpy setuptools
我有一个C扩展项目需要numpy.理想情况下,我希望下载我的项目的人能够运行python setup.py install或使用一个电话pip.我setup.py遇到的问题是,我需要导入numpy来获取头文件的位置,但我希望numpy只是一个常规要求,install_requires以便从Python包索引中自动下载.
以下是我正在尝试做的一个示例:
from setuptools import setup, Extension
import numpy as np
ext_modules = [Extension('vme', ['vme.c'], extra_link_args=['-lvme'],
include_dirs=[np.get_include()])]
setup(name='vme',
version='0.1',
description='Module for communicating over VME with CAEN digitizers.',
ext_modules=ext_modules,
install_requires=['numpy','pyzmq', 'Sphinx'])
Run Code Online (Sandbox Code Playgroud)
显然,import numpy在安装之前我不能处于顶端.我已经看到一个setup_requires参数传递给setup()但是找不到任何关于它的用途的文档.
这可能吗?
col*_*fix 28
以下至少使用numpy1.8和python {2.6,2.7,3.3}:
from setuptools import setup
from setuptools.command.build_ext import build_ext as _build_ext
class build_ext(_build_ext):
def finalize_options(self):
_build_ext.finalize_options(self)
# Prevent numpy from thinking it is still in its setup process:
__builtins__.__NUMPY_SETUP__ = False
import numpy
self.include_dirs.append(numpy.get_include())
setup(
...
cmdclass={'build_ext':build_ext},
setup_requires=['numpy'],
...
)
Run Code Online (Sandbox Code Playgroud)
有一个小的解释,看看没有"黑客"它失败的原因,请看这个答案.
请注意,使用setup_requires有一个微妙的缺点:numpy不仅会在构建扩展之前进行编译,而且还会在执行时进行编译python setup.py --help.为避免这种情况,您可以检查命令行选项,如https://github.com/scipy/scipy/blob/master/setup.py#L205中所述,但另一方面我认为它不值得努力.
小智 5
我在 [这篇文章][1] 中找到了一个非常简单的解决方案:
或者你可以坚持使用https://github.com/pypa/pip/issues/5761。在这里,您在实际安装之前使用 setuptools.dist 安装 cython 和 numpy:
from setuptools import dist
dist.Distribution().fetch_build_eggs(['Cython>=0.15.1', 'numpy>=1.10'])
Run Code Online (Sandbox Code Playgroud)
对我来说效果很好!
对于需要使用 numpy(对于 distutils 或 get_include)的包来说,这是一个基本问题。我不知道如何使用 pip 或简易安装“引导”它。
但是,为您的模块制作 conda 包并提供依赖项列表很容易,以便有人只需执行 conda install pkg-name 即可下载并安装所需的所有内容。
Conda 可在 Anaconda 或 Miniconda(python + conda)中使用。
请参阅此网站:http://docs.continuum.io/conda/index.html 或此幻灯片了解更多信息: https://speakerdeck.com/teoliphant/packaging-and-deployment-with-conda
| 归档时间: |
|
| 查看次数: |
7051 次 |
| 最近记录: |