对distutils的贡献
distcontrib的Python项目详细描述
python包distcontrib为distutils提供实用程序函数,扩展其 功能,如与cython构建的集成和doctest的启动程序。
<^ {tt1}$存在的主要原因是使生活变得简单多了。 为项目编写setup.py时。您可以创建模板setup.py 文件,并简单地复制到所有新的或现有的项目,你没有任何 修改,在大多数情况下。在封面下,distcontrib找到几个 关于您的项目和automatically自我配置的零碎信息 这样就不必每次创建新文件时都调整setup.py文件 项目。另请参见:distcontrib-migrate
用法
这是您的setup.py的外观示例:
#!/usr/bin/env python from setuptools import find_packages from distutils.core import setup from Cython.Distutils import build_ext as cython_build import distcontrib as du ## # This block contains settings you will eventually need to change ### import myapp as myapp #--- adjust to your package name PACKAGE = myapp.pkg_name VERSION = myapp.pkg_version DESCRIPTION = myapp.pkg_description LICENSE = myapp.pkg_license URL = myapp.pkg_url AUTHOR = myapp.pkg_author AUTHOR_EMAIL = myapp.pkg_email KEYWORDS = myapp.pkg_keywords REQUIREMENTS = myapp.pkg_requirements LONG_DESCRIPTION = du.tools.read('README') CLASSIFIERS = [ 'License :: ' + LICENSE, 'Operating System :: OS Independent', 'Programming Language :: Python', 'Programming Language :: Cython', 'Development Status :: 3 - Alpha', 'Intended Audience :: Developers', 'Environment :: Console' ] ## # From this point on, it's unlikely you will be changing anything. ### PACKAGES = find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]) PACKAGES_DATA = du.tools.findall_package_data(PACKAGES) EXT_MODULES = du.tools.find_ext_modules(PACKAGES) setup( name=PACKAGE, version=VERSION, description=DESCRIPTION, url=URL, author=AUTHOR, author_email=AUTHOR_EMAIL, long_description=LONG_DESCRIPTION, license=LICENSE, keywords=KEYWORDS, classifiers=CLASSIFIERS, packages=PACKAGES, package_data=PACKAGES_DATA, cmdclass={ 'build_ext' : cython_build, 'doctest' : du.doctest, 'zap' : du.zap, }, ext_modules=EXT_MODULES, install_requires=REQUIREMENTS )
然后在myapp/__init__.py文件下创建如下内容:
#!/usr/bin/env python pkg_name = __name__ if __package__ is None else __package__ pkg_description = 'This application does everything you can imagine' pkg_version = '0.1.0' pkg_license = 'OSI Approved :: BSD License' pkg_url = 'http://' + pkg_name + '.readthedocs.org/' pkg_author = 'Richard Gomes http://rgomes-info.blogspot.com' pkg_email = 'rgomes.info@gmail.com' pkg_keywords = [ 'artificial','intelligence','magic','sorcery','voodoo' ] pkg_requirements = [ 'lxml', 'sqlalchemy' ]
然后您可以从命令行输入:
$ python setup.py zap # clean on steroids $ python setup.py doctest # run your doctests $ python setup.py build_ext # build with Cython
命令zap比命令clean清除的内容要多得多,这是将更改提交到源代码管理或创建工作文件夹备份副本之前的理想步骤。
命令doctest运行所有包中的所有doctest。如果您发现这些是未运行的doctest,请确保您已在所有包中创建了__init__.py文件。
特殊情况
在某些情况下,您可能必须保证您的setup.py安装了一个最小的基本需求集,如果没有安装,可能会阻止您的setup.py正常运行。通过从包distcontrib.bootstrap中借用函数install_requirements并在setup.py的顶部调用它,您可以安装这些基本需求,如下所示:
#!/usr/bin/env python ESSENTIAL = [ 'distribute', 'version', 'Cython', 'distcontrib', 'distcontrib-migrate' ] # This function was copied verbatim from distcontrib.bootstrap # In certain situations, you are not sure if distcontrib is installed, then # makes sense to have this function straight on the top of your setup.py def install_requirements(requirements, verbose=True): import os, pip pip_args = list() if verbose: print('Installing requirements: ' + str(requirements)) pip_args.append( '--verbose' ) proxy = os.environ['http_proxy'] if proxy: pip_args.append('--proxy') pip_args.append(proxy) if verbose: print('http_proxy=' + proxy) pip_args.append('install') for req in requirements: pip_args.append( req ) pip.main(initial_args = pip_args) try: from setuptools import find_packages from distutils.core import setup from Cython.Distutils import build_ext as cython_build import distcontrib as du import distcontrib_migrate as dm except: #-- import distcontrib.bootstrap #-- distcontrib.bootstrap.install_requirements( ESSENTIAL ) install_requirements( ESSENTIAL ) # do it again from setuptools import find_packages from distutils.core import setup from Cython.Distutils import build_ext as cython_build import distcontrib as du import distcontrib_migrate as dm ... the rest of your setup.py comes here