首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cmdclass在Pythons中做什么?

cmdclass在Pythons中做什么?
EN

Stack Overflow用户
提问于 2015-01-07 10:21:50
回答 2查看 13.3K关注 0票数 21

我最近收到了一个请求,它增加了

代码语言:javascript
复制
class build_ext(_build_ext):
    'to install numpy'
    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.py,结果是:

代码语言:javascript
复制
from setuptools.command.build_ext import build_ext as _build_ext

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup


class build_ext(_build_ext):
    'to install numpy'
    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())


config = {
    'cmdclass':{'build_ext':build_ext}, #numpy hack
    'setup_requires':['numpy'],         #numpy hack
    'name': 'nntoolkit',
    'version': '0.1.25',
    'author': 'Martin Thoma',
    'author_email': 'info@martin-thoma.de',
    'packages': ['nntoolkit'],
    'scripts': ['bin/nntoolkit'],
    'url': 'https://github.com/MartinThoma/nntoolkit',
    'license': 'MIT',
    'description': 'Neural Network Toolkit',
    'long_description': """...""",
    'install_requires': [
        "argparse",
        "theano",
        "nose",
        "natsort",
        "PyYAML",
        "matplotlib",
        "h5py",
        "numpy",
        "Cython"
    ],
    'keywords': ['Neural Networks', 'Feed-Forward', 'NN', 'MLP'],
    'download_url': 'https://github.com/MartinThoma/nntoolkit',
    'classifiers': ['Development Status :: 3 - Alpha'],
    'zip_safe': False,
    'test_suite': 'nose.collector'
}

setup(**config)

是干什么的呢?

文档只声明:

cmdclass:命令名到命令子类的映射(字典)

EN

回答 2

Stack Overflow用户

发布于 2015-01-07 13:29:37

Numpy库是用C/C++编写的。因此,与其他包不同的是,它需要在实际调用它们之前进行编译。所以'build_ext‘只是编译它们。

博客详细信息:http://sadafnoor.me/blog/how-to-automate-numpy-installation-in-your-project-using-setuptool/

票数 7
EN

Stack Overflow用户

发布于 2022-10-20 20:18:19

所有的骗局:

我发现另一个博客很好地解释了这一点:

https://niteo.co/blog/setuptools-run-custom-code-in-setup-py

对studioj也大喊大叫;这一页解释了同样的事情,但是对于distutils而不是setuptools (它们的定义非常相似)。

本质上,它们创建类来覆盖setuptools/distutils使用的各种命令(特别是它们的".run“命令)。

例如,如果要在setuptools中覆盖"build“命令,可以使用如下模式:

代码语言:javascript
复制
from setuptools import setup
from setuptools.command.build import build

class MyBuild(build):
    def run(self):
        # ---- some custom code here ----
        # important to do this instead of "super", since it hasn't been properly updated
        build.run(self) 

setup(
    # setup specifications here
    cmdclass={"build": MyBuild}
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27817190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档