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:\Demo\foo\__init__.py

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

3〉cmd进入C:\Demo,执行python setup.py bdist_egg,会在C:\demo\dist下找到生成的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-info\PKG-INFO
writing top-level names to foo.egg-info\top_level.txt
writing dependency_links to foo.egg-info\dependency_links.txt
writing manifest file 'foo.egg-info\SOURCES.txt'
reading manifest file 'foo.egg-info\SOURCES.txt'
writing manifest file 'foo.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\foo
copying foo\__init__.py -> build\lib\foo
creating build\bdist.win32
creating build\bdist.win32\egg
creating build\bdist.win32\egg\foo
copying build\lib\foo\__init__.py -> build\bdist.win32\egg\foo
byte-compiling build\bdist.win32\egg\foo\__init__.py to __init__.pyc
creating build\bdist.win32\egg\EGG-INFO
copying foo.egg-info\PKG-INFO -> build\bdist.win32\egg\EGG-INFO
copying foo.egg-info\SOURCES.txt -> build\bdist.win32\egg\EGG-INFO
copying foo.egg-info\dependency_links.txt -> build\bdist.win32\egg\EGG-INFO
copying foo.egg-info\top_level.txt -> build\bdist.win32\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist\foo-0.0.1-py2.7.egg' and adding 'build\bdist.win32\egg' to it
removing 'build\bdist.win32\egg' (and everything under it)

C:\Demo>

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

 C:\Demo\dist 的目录

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:\Demo\dist>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:\python27\lib\site-packages
Adding foo 0.0.1 to easy-install.pth file

Installed c:\python27\lib\site-packages\foo-0.0.1-py2.7.egg
Processing dependencies for foo==0.0.1
Finished processing dependencies for foo==0.0.1

C:\Demo\dist>
从安装的过程来看,egg包是安装在
c:\python27\lib\site-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

你可能感兴趣的:(python,easy_install,setuptools)