一、编写模块
进入项目目录
#cd /home/pysetup
#vi foo.py
class MyClass():
def __init__(self):
self.blog = "http://blog.liuts.com"
def printblog(self):
print self.blog
def printBblog(self):
print self.blog.swapcase()
二、编写setup.py
#vi setup.py
from distutils.core import setup
setup(name='Myblog',
version='1.0',
description='My Blog Distribution Utilities',
author='Liu tiansi',
author_email='liutiansi@gmail.com',
url='http://blog.liuts.com',
py_modules=['foo'],
)
#python setup.py build # 编译
#python setup.py install #安装
#python setup.py sdist #生成压缩包(zip/tar.gz)
#python setup.py bdist_wininst #生成NT平台安装包(.exe)
#python setup.py bdist_rpm #生成rpm包
或者直接"bdist 包格式",格式如下:
#python setup.py bdist --help-formats
--formats=rpm RPM distribution
--formats=gztar gzip'ed tar file
--formats=bztar bzip2'ed tar file
--formats=ztar compressed tar file
--formats=tar tar file
--formats=wininst Windows executable installer
--formats=zip ZIP file
四、打包
#python setup.py sdist
running sdist
running check
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 Myblog-1.0
making hard links in Myblog-1.0...
hard linking foo.py -> Myblog-1.0
hard linking setup.py -> Myblog-1.0
creating dist
Creating tar archive
removing 'Myblog-1.0' (and everything under it)
#ls dist
Myblog-1.0.tar.gz
五、安装
#tar -zxvf Myblog-1.0.tar.gz
#cd Myblog-1.0.tar.gz
#python setup.py install (此命令大家再熟悉不过了)
running install
running build
running build_py
creating build/lib.linux-x86_64-2.6
copying foo.py -> build/lib.linux-x86_64-2.6
running install_lib
copying build/lib.linux-x86_64-2.6/foo.py -> /usr/local/lib/python2.6/dist-packages
byte-compiling /usr/local/lib/python2.6/dist-packages/foo.py to foo.pyc
running install_egg_info
Writing /usr/local/lib/python2.6/dist-packages/Myblog-1.0.egg-info
六、测试
root@zhukai-Inspiron-3420:/home/zhukai/桌面/Myblog-1.0# python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from foo import MyClass
>>> app=MyClass()
>>> app.printblog
<bound method MyClass.printblog of <foo.MyClass instance at 0x7f3607d79440>>
>>> app.printblog()
http://www.baidu.com
>>> app.printcaseblog()
HTTP://WWW.BAIDU.COM
>>>