python的第三方模塊越來越豐富,涉及的領(lǐng)域也非常廣,如科學(xué)計算、圖片處理、web應(yīng)用、GUI開發(fā)等。當(dāng)然也可以將自己寫的模塊進(jìn)行打包或發(fā)布。一簡單的方法是將你的類包直接copy到python的lib目錄,但此方式不便于管理與維護(hù),存在多個python版本時會非常混亂?,F(xiàn)介紹如何編寫setup.py來對一個簡單的python模塊進(jìn)行打包。
一、編寫模塊
進(jìn)入項目目錄
#cd /home/pysetup
#vi foo.py
#! /usr/bin/env python
#coding=utf-8
class MyLib():
def __init__(self):
self.str = "hello!"
def print_log(self):
print self.str
def printBlog(self):
print self.str.swapcase();
二、編寫setup.py
#vi setup.py
#! /usr/bin/env python
#coding=utf-8
from distutils.coreimport setup
setup(
name='MyLib',
version='1.0',
description='My Lib disribution Utility',
author='Edison',
author_email='eddy.wd5@gmail.com',
url='http://hi.baidu.com/gylxue',
py_modules=['mylib'],
)
更多參數(shù)說明見表:
三、setup.py參數(shù)說明
#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
warning: sdist: manifest template 'MANIFEST.in' does not exist (using defaultfile list)
warning: sdist: standard file not found: should have one of README, README.txt
writing manifest file 'MANIFEST'
creating MyLib -1.0
making hard links in MyLib -1.0...
hard linking foo.py -> MyLib -1.0
hard linking setup.py -> MyLib -1.0
creating dist
tar -cf dist/ MyLib -1.0.tar MyLib -1.0
gzip -f9 dist/MyLib -1.0.tar
removing ‘MyLib -1.0' (and everything under it)
提示兩條warning可以忽略,不影響打包,當(dāng)然一個完善的項目必須有README及MANIFEST.in(項目文件清單)文件。
#ls dist
MyLib -1.0.tar.gz
五、安裝
#tar -zxvf MyLib -1.0.tar.gz
#cd MyLib -1.0.tar.gz
#python setup.py install (此命令大家再熟悉不過了)
running install
running build
running build_py
creating build/lib.linux-x86_64-2.6
copying mylib.py -> build/lib.linux-x86_64-2.6
running install_lib
copying build/lib.linux-x86_64-2.6/ mylib.py ->/usr/local/lib/python2.6/dist-packages
byte-compiling /usr/local/lib/python2.6/dist-packages/ mylib.py to mylib.pyc
running install_egg_info
Writing /usr/local/lib/python2.6/dist-packages/ mylib -1.0.egg-info
六、測試
If your package provider didn't produce a setup.pyuninstall method then,more often than not,
you can just manually remove the package fromyour Python's site-packages directory.
This will be located at /usr/lib/python2.5/site-packages or equivalent for your distro and version ofPython.
Within that there will be either a directoryor a .eggfile corresponding to the package's name.
Simply delete that.There are some instanceswhere packages will install stuff elsewhere.
Django for instance installs django-admin.py to /usr/sbin. Y
一些插件(例如 SpamFilter)可以作為.egg文件進(jìn)行下載, 可以和easy_install程序一起安裝:
easy_install TracSpamFilter如果easy_install不在你的系統(tǒng)上,請參考上節(jié)中的要求來安裝. Windows用戶還需要將Python安裝包的Scripts目錄, 例如C:\Python23\Scripts, 添加到PATH環(huán)境變量中. 更多信息,請參考 easy_install Windows說明.
如果安裝完一個egg后, Trac報告權(quán)限錯誤, 而你不想為Web服務(wù)器提供一個可寫的egg緩存目錄,你只需解壓這個egg來繞開這個問題.使用--always-unzip選項:
easy_install --always-unzip TracSpamFilter-0.2.1dev_r5943-py2.4.egg你應(yīng)該用與egg相同的名字作為目錄名(包括結(jié)尾的.egg), 目錄中是解壓后的內(nèi)容.
Trac也會搜索全局安裝的插件(自0.10版本后),參見 TracIni#GlobalConfiguration.
從源代碼easy_install從源代碼的快照安裝. 只需要Subversion的URL或者源代碼的壓縮包(tarball/zip):
easy_install http://svn.edgewall.com/repos/trac/sandbox/spam-filter啟用插件不像只安裝在環(huán)境目錄中的那些插件, 你需要通過trac.ini來啟用全局安裝的插件. 這是在配置文件的[components]段中完成的, 例如:
[components]tracspamfilter.* = enabled選項名是插件的Python安裝包.插件的相應(yīng)文檔中應(yīng)該明確指定, 但通過查看源代碼輕易找到(找包含__init__.py的頂級目錄).
注意:安裝完插件后,你還需要重啟Apache.
卸載easy_install 或 python setup.py 還沒有卸載功能. 然而, 刪除全局安裝插件egg的方法通常是:
運行如果你對egg的位置不確定,這里有一個小技巧來定位egg(或任意包)- 用插件使用的名字空間(跟啟用插件一樣)替換:
>>> import myplugin>>> print myplugin.__file__/opt/local/python24/lib/site-packages/myplugin-0.4.2-py2.4.egg/myplugin/__init__.pyc