Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

第 2 章 Python 包管理

目录

2.1. setup.py
2.2. distribute
2.3. pip

2.1. setup.py

http://docs.python.org/3/install/index.html

http://docs.python.org/3/distutils/index.html

创建 setup.py 如下:

# cat setup.py
from distutils.core import setup

setup (
    name = 'firewall',
    version = '1.0.0',
    py_modules = ['firewall'],
    author = 'neo.chen',
    author_email = 'netkiller@msn.com',
    description = 'Module firewall',
    url='http://netkiller.github.io/',
)
		

注意:name和py_modules这两个参数一定要与firewall.py文件名相同,否则发布安装时会发出模块文件找不到的错误.

打包操作
# python setup.py sdist
running sdist
warning: sdist: missing required meta-data: url
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)
warning: sdist: standard file not found: should have one of README, README.txt
writing manifest file 'MANIFEST'
creating firewall-1.0.0
making hard links in firewall-1.0.0...
hard linking firewall.py -> firewall-1.0.0
hard linking setup.py -> firewall-1.0.0
creating dist
tar -cf dist/firewall-1.0.0.tar firewall-1.0.0
gzip -f9 dist/firewall-1.0.0.tar
tar -cf dist/firewall-1.0.0.tar firewall-1.0.0
gzip -f9 dist/firewall-1.0.0.tar
removing 'firewall-1.0.0' (and everything under it)
		

sdist 生成的文件

# ll dist/
total 4
-rw-r--r--. 1 root root 2123 Aug  9 12:41 firewall-1.0.0.tar.gz
		

安装包

# python setup.py install
running install
running build
running build_py
creating build
creating build/lib
copying firewall.py -> build/lib
running install_lib
copying build/lib/firewall.py -> /usr/lib/python2.6/site-packages
byte-compiling /usr/lib/python2.6/site-packages/firewall.py to firewall.pyc
running install_egg_info
Writing /usr/lib/python2.6/site-packages/firewall-1.0.0-py2.6.egg-info
		

安装后

# ll /usr/lib/python2.6/site-packages/firewall*
-rw-r--r--. 1 root root   201 Aug  9 12:42 /usr/lib/python2.6/site-packages/firewall-1.0.0-py2.6.egg-info
-rw-r--r--. 1 root root  6145 Aug  9 11:28 /usr/lib/python2.6/site-packages/firewall.py
-rw-r--r--. 1 root root 11858 Aug  9 12:42 /usr/lib/python2.6/site-packages/firewall.pyc