setuptools的使用

1.什么是setuptools

setuptoolssetuptools是 Python Enterprise Application Kit(PEAK)的一个副项目,是Python distutils增强版的集合,它可以帮助我们更简单的创建和分发Python包,尤其是拥有依赖关系的。Python还可以帮助我们管理第三方依赖包。

2.安装setuptools

我们使用ez_setup.py来安装,这是 setuptools 自豪的一种安装方式,会自动下载匹配当前Python版本的安装包来安装。

ez_setup.py文件:http://download.csdn.net/detail/chenjintaoxp/6399857

Window中cmd下执行 python ez_setup.py

3利用setuptools制作egg

1〉在C:Demo下创建setup.py . (Demo相当于工程)

#from distutils.core import setup
from setuptools import setup, find_packages
setup(
   name = 'foo',#包名
   version = '0.0.1',#版本号
   packages = find_packages()#搜索Demo下的包
)

2〉C:Demo创建包foo,C:Demofoo\__init__.py

#!/usr/bin/python
def say():
    print 'hello'
    
if __name__ == '__main__':
    say()


3〉cmd进入C:Demo,执行python setup.py bdist_egg,会在C:demodist下找到生成的egg文件:foo-0.0.1-py2.7.egg (Python2.7环境)


C:Demo 的目录

2013/10/15  00:10    <DIR>          .
2013/10/15  00:10    <DIR>          ..
2013/10/15  00:11    <DIR>          foo
2013/10/15  00:10               173 setup.py
               1 个文件            173 字节
               3 个目录 15,546,511,360 可用字节

C:Demo>python setup.py bdist_egg
running bdist_egg
running egg_info
creating foo.egg-info
writing foo.egg-infoPKG-INFO
writing top-level names to foo.egg-info	op_level.txt
writing dependency_links to foo.egg-infodependency_links.txt
writing manifest file 'foo.egg-infoSOURCES.txt'
reading manifest file 'foo.egg-infoSOURCES.txt'
writing manifest file 'foo.egg-infoSOURCES.txt'
installing library code to builddist.win32egg
running install_lib
running build_py
creating build
creating buildlib
creating buildlibfoo
copying foo\__init__.py -> buildlibfoo
creating builddist.win32
creating builddist.win32egg
creating builddist.win32eggfoo
copying buildlibfoo\__init__.py -> builddist.win32eggfoo
byte-compiling builddist.win32eggfoo\__init__.py to __init__.pyc
creating builddist.win32eggEGG-INFO
copying foo.egg-infoPKG-INFO -> builddist.win32eggEGG-INFO
copying foo.egg-infoSOURCES.txt -> builddist.win32eggEGG-INFO
copying foo.egg-infodependency_links.txt -> builddist.win32eggEGG-INFO
copying foo.egg-info	op_level.txt -> builddist.win32eggEGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'distfoo-0.0.1-py2.7.egg' and adding 'builddist.win32egg' to it
removing 'builddist.win32egg' (and everything under it)

C:Demo>


4〉利用easy_install来安装生成的 foo-0.0.1-py2.7.egg

 C:Demodist 的目录

2013/10/15  00:13    <DIR>          .
2013/10/15  00:13    <DIR>          ..
2013/10/15  00:13             1,334 foo-0.0.1-py2.7.egg
               1 个文件          1,334 字节
               2 个目录 15,546,499,072 可用字节

C:Demodist>easy_install foo-0.0.1-py2.7.egg
Processing foo-0.0.1-py2.7.egg
Copying foo-0.0.1-py2.7.egg to c:python27libsite-packages
Adding foo 0.0.1 to easy-install.pth file

Installed c:python27libsite-packagesfoo-0.0.1-py2.7.egg
Processing dependencies for foo==0.0.1
Finished processing dependencies for foo==0.0.1

C:Demodist>

从安装的过程来看,egg包是安装在

c:python27libsite-packages


5〉测试结果如下

In [1]: import foo

In [2]: foo.say()
hello

In [3]:

说明foo包已经安装成功。

foo包的Demo工程地址:http://download.csdn.net/detail/chenjintaoxp/6400019

4.使用setuptools的easy_install(管理第三方包)

安装一个包:
#通过包名安装一个普通的包(从PyPI搜索自动安装)
ex:easy_install SQLObject
#在指定的下载URL中,通过包名安装和更新包
ex:easy_install -f http://pythonpaste.org/package_index.html SQLObject
#通过source发布的URL,下载,编译,安装包
ex:easy_install http://example.com/path/to/MyPackage-1.2.3.tgz
#通过egg文件去安装包(如:foo包的安装)
ex:easy_install /my_downloads/OtherPackage-3.2.1-py2.3.egg
#更新已经安装的包到最近版本
ex:easy_install --upgrade PyProtocols

更新一个包:
#将目前包更新到2.0版本
ex:easy_install "SomePackage==2.0"
#将目前包更新到>2.0的一个版本
ex:easy_install "SomePackage>2.0"
#更新到包的最新版本
ex:easy_install --upgrade SomePackage
#通过URL来更新
ex:easy_install -f http://example.com/downloads ExamplePackage
ex:easy_install http://example.com/downloads/ExamplePackage-2.0-py2.4.egg
ex:easy_install my_downloads/ExamplePackage-2.0.tgz

卸载一个包:
#通过包名卸载包
ex:easy_install -mxN PackageName

详细easy_install使用参照:http://peak.telecommunity.com/DevCenter/EasyInstall

原文地址:https://www.cnblogs.com/pangblog/p/3370835.html