Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1748254
  • 博文数量: 290
  • 博客积分: 10653
  • 博客等级: 上将
  • 技术积分: 3178
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-24 23:08
文章存档

2013年(6)

2012年(15)

2011年(25)

2010年(86)

2009年(52)

2008年(66)

2007年(40)

分类: Python/Ruby

2013-01-09 16:29:00

python的第三方模块越来越丰富,涉及的领域也非常广,如科学计算、图片处理、web应用、GUI开发等。当然也可以将自己写的模块进行打包或发布。一简单的方法是将你的类包直接copy到python的lib目录,但此方式不便于管理与维护,存在多个python版本时会非常混乱。现介绍如何编写setup.py来对一个简单的python模块进行打包。

一、编写模块
进入项目目录
#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='',

    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包


或者直接"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可以忽略,不影响打包,当然一个完善的项目必须有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


六、测试


>>> from mylib import MyLib
>>> app= MyLib ()
>>> app.

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


一些插件(例如)可以作为.egg文件进行下载, 可以和easy_install程序一起安装:

easy_install TracSpamFilter

如果easy_install不在你的系统上,请参考上节中的要求来安装. Windows用户还需要将Python安装包的Scripts目录, 例如C:\Python23\Scripts, 添加到PATH环境变量中. 更多信息,请参考.

如果安装完一个egg后, Trac报告权限错误, 而你不想为Web服务器提供一个可写的egg缓存目录,你只需解压这个egg来绕开这个问题.使用--always-unzip选项:

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 启用插件

不像只安装在环境目录中的那些插件, 你需要通过来启用全局安装的插件. 这是在配置文件的[components]段中完成的, 例如:

[components]tracspamfilter.* = enabled

选项名是插件的Python安装包.插件的相应文档中应该明确指定, 但通过查看源代码轻易找到(找包含__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


阅读(4515) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~