setup.py文件
1.setup.py文件的作用
from setuptools import setup, find_packages
setup(
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 sdist
running sdist
running egg_info
creating dirbot. egg-info
writing dirbot. egg-info \ PKG-INFO
writing top-level names to dirbot. egg-info \ top_level. txt
writing dependency_links to dirbot. egg-info \ dependency_links. txt
writing entry points to dirbot. egg-info \ entry_points. txt
writing manifest file 'dirbot.egg-info\SOURCES.txt'
reading manifest file 'dirbot.egg-info\SOURCES.txt'
writing manifest file 'dirbot.egg-info\SOURCES.txt'
running check
warning: check: missing required meta-data : url
warning: check: missing meta-data : either (author and author_email) or (maintainer and maintainer_email) must be supplied
creating dirbot- 1.0
creating dirbot- 1.0 \ dirbot
creating dirbot- 1.0 \ dirbot. egg-info
creating dirbot- 1.0 \ dirbot\ spiders
copying files to dirbot- 1.0 ...
copying README. rst -> dirbot- 1.0
copying setup. py -> dirbot- 1.0
copying dirbot\ __init__. py -> dirbot- 1.0 \ dirbot
copying dirbot\ items. py -> dirbot- 1.0 \ dirbot
copying dirbot\ pipelines. py -> dirbot- 1.0 \ dirbot
copying dirbot\ settings. py -> dirbot- 1.0 \ dirbot
copying dirbot. egg-info \ PKG-INFO -> dirbot- 1.0 \ dirbot. egg-info
copying dirbot. egg-info \ SOURCES. txt -> dirbot- 1.0 \ dirbot. egg-info
copying dirbot. egg-info \ dependency_links. txt -> dirbot- 1.0 \ dirbot. egg-info
copying dirbot. egg-info \ entry_points. txt -> dirbot- 1.0 \ dirbot. egg-info
copying dirbot. egg-info \ top_level. txt -> dirbot- 1.0 \ dirbot. egg-info
copying dirbot\ spiders\ __init__. py -> dirbot- 1.0 \ dirbot\ spiders
copying dirbot\ spiders\ dmoz. py -> dirbot- 1.0 \ dirbot\ spiders
Writing dirbot- 1.0 \ setup. cfg
creating dist
Creating tar archive
removing 'dirbot-1.0' (and everything under it)
在当前目录下生成这样文件目录:
dist\
dirbot-1.0.tar.gz
dirbot.egg-info\
dependency_links.txt
entry_points.txt
PKG-INFO
SOURCES.txt
top_level.txt
运行python setup.py install为安装模块
running install
running bdist_egg
running egg_info
writing dirbot.egg-info\PKG -INFO
writing top-level names to dirbot.egg-info\top _level.txt
writing dependency_links to dirbot.egg-info\dependency _links.txt
writing entry points to dirbot.egg-info\entry _points.txt
reading manifest file 'dirbot.egg-info\SOURCES .txt'
writing manifest file 'dirbot.egg-info\SOURCES .txt'
installing library code to build\bdist .win32\egg
running install_lib
running build_py
creating build
creating build\lib
creating build\lib \dirbot
copying dirbot\items .py -> build\lib \dirbot
copying dirbot\pipelines .py -> build\lib \dirbot
copying dirbot\settings .py -> build\lib \dirbot
copying dirbot\_ _init__.py -> build\lib \dirbot
creating build\lib \dirbot \spiders
copying dirbot\spiders \dmoz .py -> build\lib \dirbot \spiders
copying dirbot\spiders \_ _init__.py -> build\lib \dirbot \spiders
creating build\bdist .win32
creating build\bdist .win32\egg
creating build\bdist .win32\egg \dirbot
copying build\lib \dirbot \items .py -> build\bdist .win32\egg \dirbot
copying build\lib \dirbot \pipelines .py -> build\bdist .win32\egg \dirbot
copying build\lib \dirbot \settings .py -> build\bdist .win32\egg \dirbot
creating build\bdist .win32\egg \dirbot \spiders
copying build\lib \dirbot \spiders \dmoz .py -> build\bdist .win32\egg \dirbot \spiders
copying build\lib \dirbot \spiders \_ _init__.py -> build\bdist .win32\egg \dirbot \spiders
copying build\lib \dirbot \_ _init__.py -> build\bdist .win32\egg \dirbot
byte-compiling build\bdist .win32\egg \dirbot \items .py to items.pyc
byte-compiling build\bdist .win32\egg \dirbot \pipelines .py to pipelines.pyc
byte-compiling build\bdist .win32\egg \dirbot \settings .py to settings.pyc
byte-compiling build\bdist .win32\egg \dirbot \spiders \dmoz .py to dmoz.pyc
byte-compiling build\bdist .win32\egg \dirbot \spiders \_ _init__.py to __init__.pyc
byte-compiling build\bdist .win32\egg \dirbot \_ _init__.py to __init__.pyc
creating build\bdist .win32\egg \EGG -INFO
copying dirbot.egg-info\PKG -INFO -> build\bdist .win32\egg \EGG -INFO
copying dirbot.egg-info\SOURCES .txt -> build\bdist .win32\egg \EGG -INFO
copying dirbot.egg-info\dependency _links.txt -> build\bdist .win32\egg \EGG -INFO
copying dirbot.egg-info\entry _points.txt -> build\bdist .win32\egg \EGG -INFO
copying dirbot.egg-info\top _level.txt -> build\bdist .win32\egg \EGG -INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist\dirbot -1.0-py2.7.egg' and adding 'build\bdist .win32\egg ' to it
removing 'build\bdist .win32\egg ' (and everything under it)
Processing dirbot-1.0-py2.7.egg
Copying dirbot-1.0-py2.7.egg to c:\python 27\lib \site -packages
Adding dirbot 1.0 to easy-install.pth file
Installed c:\python 27\lib \site -packages\dirbot -1.0-py2.7.egg
Processing dependencies for dirbot==1.0
Finished 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-script
8、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指定的文件
from setuptools import setup, find_packages
setup(
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.
)
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信息中。