制作python模块安装包(转)
2010-12-03 22:39

python的第三方模块越来越丰富,涉及的领域也非常广,如科学计算、图片处理、web应用、GUI开发等。当然也可以将自己写的模块进行打包或发布。一简单的方法是将你的类包直接copypythonlib目录,但此方式不便于管理与维护,存在多个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.core
import 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'],

)

更多参数说明见表:

三、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 default
file 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可以忽略,不影响打包,当然一个完善的项目必须有READMEMANIFEST.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.py
uninstall
method then,
more often than not,

you can just manually remove the package from
your Python's
site-packages directory.

This will be located at /usr/lib/python2.5/site-packages or equivalent for your distro and version of
Python.

Within that there will be either a directory
or a
.egg
file corresponding to the package's name.

Simply delete that.There are some instances
where packages will install stuff elsewhere.

Django for instance installs django-admin.py to /usr/sbin. Y

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

easy_install TracSpamFilter

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

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

easy_install --always-unzip TracSpamFilter-0.2.1dev_r5943-py2.4.egg

你应该用与egg相同的名字作为目录名(包括结尾的.egg), 目录中是解压后的内容.

Trac也会搜索全局安装的插件(0.10版本后),
参见TracIni#GlobalConfiguration.

从源代码

easy_install从源代码的快照安装. 只需要SubversionURL或者源代码的压缩包(tarball/zip):

easy_install http://svn.edgewall.com/repos/trac/sandbox/spam-filter

启用插件

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

[components]tracspamfilter.* = enabled

选项名是插件的Python安装包.
插件的相应文档中应该明确指定, 但通过查看源代码轻易找到(找包含__init__.py的顶级目录).

注意:安装完插件后,
你还需要重启Apache.

卸载

easy_installpython 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
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。