setup.py文件

来源:互联网 发布:香港专业教育学院 知乎 编辑:程序博客网 时间:2024/05/16 23:54

setup.py文件

1.setup.py文件的作用

#setup.pyfrom setuptools import setup, find_packagessetup(    name='dirbot',    version='1.0',    packages=find_packages(),    entry_points={'scrapy': ['settings = dirbot.settings']},)

dirbot项目目录

dirbot\    dirlot\        spiders\            __init__.py            dmoz.py        __init__.py        items.py        pipelines.py        settings.py    .gitignore    README.rst    scrapy.cfg    setup.py

运行python setup.py sdist为模块创建一个源码包

#python setup.py sdistrunning sdistrunning egg_infocreating dirbot.egg-infowriting dirbot.egg-info\PKG-INFOwriting top-level names to dirbot.egg-info\top_level.txtwriting dependency_links to dirbot.egg-info\dependency_links.txtwriting entry points to dirbot.egg-info\entry_points.txtwriting manifest file 'dirbot.egg-info\SOURCES.txt'reading manifest file 'dirbot.egg-info\SOURCES.txt'writing manifest file 'dirbot.egg-info\SOURCES.txt'running checkwarning: check: missing required meta-data: urlwarning: check: missing meta-data: either (author and author_email) or (maintainer and maintainer_email) must be suppliedcreating dirbot-1.0creating dirbot-1.0\dirbotcreating dirbot-1.0\dirbot.egg-infocreating dirbot-1.0\dirbot\spiderscopying files to dirbot-1.0...copying README.rst -> dirbot-1.0copying setup.py -> dirbot-1.0copying dirbot\__init__.py -> dirbot-1.0\dirbotcopying dirbot\items.py -> dirbot-1.0\dirbotcopying dirbot\pipelines.py -> dirbot-1.0\dirbotcopying dirbot\settings.py -> dirbot-1.0\dirbotcopying dirbot.egg-info\PKG-INFO -> dirbot-1.0\dirbot.egg-infocopying dirbot.egg-info\SOURCES.txt -> dirbot-1.0\dirbot.egg-infocopying dirbot.egg-info\dependency_links.txt -> dirbot-1.0\dirbot.egg-infocopying dirbot.egg-info\entry_points.txt -> dirbot-1.0\dirbot.egg-infocopying dirbot.egg-info\top_level.txt -> dirbot-1.0\dirbot.egg-infocopying dirbot\spiders\__init__.py -> dirbot-1.0\dirbot\spiderscopying dirbot\spiders\dmoz.py -> dirbot-1.0\dirbot\spidersWriting dirbot-1.0\setup.cfgcreating distCreating tar archiveremoving 'dirbot-1.0' (and everything under it)

在当前目录下生成这样文件目录:

dist\    dirbot-1.0.tar.gzdirbot.egg-info\    dependency_links.txt    entry_points.txt    PKG-INFO    SOURCES.txt    top_level.txt

在当前目录下,会创建dist目录,里面有个文件名为dirbot-1.0.tar.gz,这个就是可以分发的包(如果使用命令python setup.py bdist_egg,那么会在dist目录中生成dirbot-1.0-py2.7.egg包,setup.py中第一句引入需要改为from setuptools import setup)。使用者拿到这个包后,解压,到dirbot-1.0目录下执行:python setup.py install,那么,ditbot.py就会被拷贝到python类路径下,可以被导入使用(如果安装是egg文件,会把egg文件拷贝到dist-packages目录下)。

运行python setup.py install为安装模块

running installrunning bdist_eggrunning egg_infowriting dirbot.egg-info\PKG-INFOwriting top-level names to dirbot.egg-info\top_level.txtwriting dependency_links to dirbot.egg-info\dependency_links.txtwriting entry points to dirbot.egg-info\entry_points.txtreading manifest file 'dirbot.egg-info\SOURCES.txt'writing manifest file 'dirbot.egg-info\SOURCES.txt'installing library code to build\bdist.win32\eggrunning install_librunning build_pycreating buildcreating build\libcreating build\lib\dirbotcopying dirbot\items.py -> build\lib\dirbotcopying dirbot\pipelines.py -> build\lib\dirbotcopying dirbot\settings.py -> build\lib\dirbotcopying dirbot\__init__.py -> build\lib\dirbotcreating build\lib\dirbot\spiderscopying dirbot\spiders\dmoz.py -> build\lib\dirbot\spiderscopying dirbot\spiders\__init__.py -> build\lib\dirbot\spiderscreating build\bdist.win32creating build\bdist.win32\eggcreating build\bdist.win32\egg\dirbotcopying build\lib\dirbot\items.py -> build\bdist.win32\egg\dirbotcopying build\lib\dirbot\pipelines.py -> build\bdist.win32\egg\dirbotcopying build\lib\dirbot\settings.py -> build\bdist.win32\egg\dirbotcreating build\bdist.win32\egg\dirbot\spiderscopying build\lib\dirbot\spiders\dmoz.py -> build\bdist.win32\egg\dirbot\spiderscopying build\lib\dirbot\spiders\__init__.py -> build\bdist.win32\egg\dirbot\spiderscopying build\lib\dirbot\__init__.py -> build\bdist.win32\egg\dirbotbyte-compiling build\bdist.win32\egg\dirbot\items.py to items.pycbyte-compiling build\bdist.win32\egg\dirbot\pipelines.py to pipelines.pycbyte-compiling build\bdist.win32\egg\dirbot\settings.py to settings.pycbyte-compiling build\bdist.win32\egg\dirbot\spiders\dmoz.py to dmoz.pycbyte-compiling build\bdist.win32\egg\dirbot\spiders\__init__.py to __init__.pycbyte-compiling build\bdist.win32\egg\dirbot\__init__.py to __init__.pyccreating build\bdist.win32\egg\EGG-INFOcopying dirbot.egg-info\PKG-INFO -> build\bdist.win32\egg\EGG-INFOcopying dirbot.egg-info\SOURCES.txt -> build\bdist.win32\egg\EGG-INFOcopying dirbot.egg-info\dependency_links.txt -> build\bdist.win32\egg\EGG-INFOcopying dirbot.egg-info\entry_points.txt -> build\bdist.win32\egg\EGG-INFOcopying dirbot.egg-info\top_level.txt -> build\bdist.win32\egg\EGG-INFOzip_safe flag not set; analyzing archive contents...creating 'dist\dirbot-1.0-py2.7.egg' and adding 'build\bdist.win32\egg' to itremoving 'build\bdist.win32\egg' (and everything under it)Processing dirbot-1.0-py2.7.eggCopying dirbot-1.0-py2.7.egg to c:\python27\lib\site-packagesAdding dirbot 1.0 to easy-install.pth fileInstalled c:\python27\lib\site-packages\dirbot-1.0-py2.7.eggProcessing dependencies for dirbot==1.0Finished processing dependencies for dirbot==1.0

安装该模块生成build文件目录

build\    bdist.win32\    lib\        dirlot\        spiders\            __init__.py            dmoz.py        __init__.py        items.py        pipelines.py        settings.py

2.setup函数还有一些参数:

1、packages告诉Distutils需要处理那些包(包含__init__.py的文件夹)2、package_dir告诉Distutils哪些目录下的文件被映射到哪个源码包,感觉好像是一个相对路径的定义。一个例子:package_dir = {'': 'lib'},表示以lib为主目录。3、ext_modules是一个包含Extension实例的列表,Extension的定义也有一些参数。4、ext_package定义extension的相对路径5、requires定义依赖哪些模块6、provides定义可以为哪些模块提供依赖7、scripts指定python源码文件,可以从命令行执行。在安装时指定--install-script8、package_data通常包含与包实现相关的一些数据文件或类似于readme的文件。
package_data = {'': ['*.txt'], 'mypkg': ['data/*.dat'],}
表示包含所有目录下的txt文件和mypkg/data目录下的所有dat文件。9、data_files指定其他的一些文件(如配置文件)
setup(...,    data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),                ('config', ['cfg/data.cfg']),                ('/etc/init.d', ['init-script'])]    )

3.执行sdist命令时,默认会打包哪些东西呢?

  • 所有由py_modules或packages指定的源码文件
  • 所有由ext_modules或libraries指定的C源码文件
  • 由scripts指定的脚本文件
  • 类似于test/test*.py的文件
  • README.txt或README,setup.py,setup.cfg
  • 所有package_data或data_files指定的文件

4.Setuptools

上面的setup.py和setup.cfg都是遵循python标准库中的Distutils,而setuptools工具针对Python官方的distutils做了很多针对性的功能增强,比如依赖检查,动态扩展等。很多高级功能我就不详述了,自己也没有用过,等用的时候再作补充。详情可参见这里。

一个典型的遵循setuptools的脚本:

from setuptools import setup, find_packagessetup(    name = "HelloWorld",    version = "0.1",    packages = find_packages(),    scripts = ['say_hello.py'],    # Project uses reStructuredText, so ensure that the docutils get    # installed or upgraded on the target machine    install_requires = ['docutils>=0.3'],    package_data = {        # If any package contains *.txt or *.rst files, include them:        '': ['*.txt', '*.rst'],        # include any *.msg files found in the 'hello' package, too:        'hello': ['*.msg'],    },    # metadata for upload to PyPI    author = "Me",    author_email = "me@example.com",    description = "This is an Example Package",    license = "PSF",    keywords = "hello world example examples",    url = "http://example.com/HelloWorld/",   # project home page, if any    # could also include long_description, download_url, classifiers, etc.)

Setuptools相对distutils,增强的关键字:

include_package_data:为True时自动添加受版本控制的数据文件,可替代package_data,同时,exclude_package_data可以排除某些文件。当你需要加入没有被版本控制的文件时,还是老老实实使用package_data吧。install_requires:代替require函数。表示当前包的安装依赖于哪些分发包,这些信息会写入egg的元信息中,这些包在安装时会自动(从PyPI)下载并安装。如果包在PyPI中找不到,则会从dependency_links标识的URL中获取。extras_require:当前包的高级/额外特性需要依赖的分发包。entry_points:这个很经典。见下面的讲解。setup_requires: 安装脚本执行时需要依赖的分发包,主要用于构建过程。注意,这里列出的包不会自动安装,如果需要,同时要在install_requires中指定。dependency_links:URL地址。这些地址在安装setup_requires或tests_require指定的包时使用。会写入egg的metadata信息中。
原创粉丝点击