Python模块安装方式有两种:
使用内置的distutils库编写setup.py,而后python setup.py即可,这需要自行从PyPI中下载所需的模块解压后执行命令安装,并且根据具体模块的实际情况,你有可能需要手动解决包之间的依赖。
另外一种就是使用easy_install了。easy_install 是PEAK出品的setuptools的一个外部命令,用于调用其内部的ez_install工具来增加Python的包管理。使用easy_install安装模块只需要执行easy_install <module_name>,easy_install就会自动从PyPI中搜索最新版本的模块并下载、编译、安装,同时也会自动解决包之间的依赖问题。当然,easy_install的能力并不仅限于此,除此之外还有更多的用法,具体的请各位参考网上的资料哈!
安装方法,到PyPI里的setuptools主页下载源码包,解压到任意目录后执行python setup.py install即可。使用前请检查系统环境变量内是否包含<python root>/Scripts。
egg文件类似于Java中的jar,是源码的一种打包格式,用于分发安装,也是PEAK的setuptools所支持的一个特性。
打包方法:在模块根目录下新建一个setup.py,输入以下内容:
from setuptools import setup, find_packages
setup(
name = "eggtest",
version = "0.1",
packages = find_packages(),
description = "egg test demo desc1",
long_description = "egg test demo desc2",
author = "zhengwei",
author_email = "test@egg.com",
licence = "GPL",
keywords = ("test", "egg"),
platforms = "Independent",
url = "http://zhengwei.name/"
)
然后执行python setup bdist_egg就会将你的模块打包至<python root>/Lib/site-packages下了。注意packages = find_packages(),此行是使用find_packages()自动查找模块下的子文件夹,如果子文件夹下有__init__.py,就作为包导入。如果要导入根目录下的模块,请指定py_modules = ["module1", "module2"]即可。
egg文件的命名格式为<name>-<version>-<python version>,上述代码生成的egg包名就为eggtest-0.1-py2.7.egg。当然,同时也支持将模块打包为zip、exe和rpm(linux only)格式,具体参见:
http://blog.csdn.net/hong201/archive/2009/05/28/4219035.aspx
http://www.ibm.com/developerworks/cn/linux/l-cppeak3.html
关于egg包的卸载,egg文件可以遗留,但是必须要移除site-packages/easy-install.pth内对应的包名,这样才能确保不能在python内继续引用相关模块,:)
参考引用:http://www.juziblog.com/?p=365001
官网文档:http://docs.python.org/library/distutils.html
setup.py参考:https://github.com/facebook/tornado/blob/master/setup.py