python的第三方模块越来越丰富,涉及的领域也非常广,如科学计算、图片处理、web应用、GUI开发等。当然也可以将自己写的模块进行打包或发布。一简单的方法是将你的类包直接copy到python的lib目录,但此方式不便于管理与维护,存在多个python版本时会非常混乱。现介绍如何编写setup.py来对一个简单的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(); #coding=utf-8 from distutils.core 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'], ) 更多参数说明见表: 三、setup.py参数说明
#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包
#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
running sdist warning: sdist: manifest template 'MANIFEST.in' does not exist (using default 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)
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 -> 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
>>> from mylib import MyLib >>> app= MyLib () >>> app. If your package provider didn't produce a setup.py you can just manually remove the package from This will be located at /usr/lib/python2.5/site-packages or equivalent for your distro and version of Within that there will be either a directory Simply delete that.There are some instances Django for instance installs django-admin.py to /usr/sbin. Y 一些插件(例如SpamFilter)可以作为.egg文件进行下载, 可以和easy_install程序一起安装: easy_install TracSpamFilter
如果easy_install不在你的系统上, 如果安装完一个egg后, Trac报告权限错误, 而你不想为Web服务器提供一个可写的egg缓存目录, easy_install --always-unzip TracSpamFilter-0.2.1dev_r5943-py2.4.egg
你应该用与egg相同的名字作为目录名(包括结尾的.egg), 目录中是解压后的内容. Trac也会搜索全局安装的插件(自0.10版本后), 从源代码 easy_install从源代码的快照安装. 只需要Subversion的URL或者源代码的压缩包(tarball/zip): easy_install http://svn.edgewall.com/repos/trac/sandbox/spam-filter
启用插件 不像只安装在环境目录中的那些插件, 你需要通过trac.ini来启用全局安装的插件. 这是在配置文件的[components]段中完成的, 例如:
[components]tracspamfilter.* = enabled
选项名是插件的Python安装包. 注意:安装完插件后, 卸载 easy_install或python setup.py还没有卸载功能. 然而, 删除全局安装插件egg的方法通常是: 运行 如果你对egg的位置不确定,
>>> import myplugin>>> print myplugin.__file__/opt/local/python24/lib/site-packages/myplugin-0.4.2-py2.4.egg/myplugin/__init__.pyc
|
制作python模块安装包(转)_Time Goes By_百度空间
制作python模块安装包(转)
2010-12-03 22:39