制作python模块安装包[原创]

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

一、编写模块
进入项目目录
#cd /home/pysetup 
#vi foo.py

  • view plainprint?
    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

view plainprint?
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'],  
     )

更多参数说明见表:

1.png

三、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 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
tar -cf dist/Myblog-1.0.tar Myblog-1.0
gzip -f9 dist/Myblog-1.0.tar
removing 'Myblog-1.0' (and everything under it)

提示两条warning可以忽略,不影响打包,当然一个完善的项目必须有README及MANIFEST.in(项目文件清单)文件。
#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

六、测试

>>> from foo import MyClass
>>> app=MyClass()
>>> app.print printblog()
>>> app.printblog()
http://blog.liuts.com
>>> app.printBblog()
HTTP://BLOG.LIUTS.COM
>>>

参考文献:
http://docs.python.org/distutils/setupscript.html

如大家有什么疑问或感兴趣的话题可以通过weibo与我交流:http://t.qq.com/yorkoliu

转自:http://blog.liuts.com/post/213/#entrymore

原创文章,作者:s19930811,如若转载,请注明出处:http://www.178linux.com/1737

(0)
s19930811s19930811
上一篇 2015-03-27 14:22
下一篇 2015-03-27 17:10

相关推荐

  • keepalived+nginx 模型示例

    原理为: 调度器 利用 keepalived 保持高可用性,实现对系统的监控和VIP 的floating NGINX 利用upstream模块进行调度 关键点: keepalived 对NGINX 状态的监控: //利用配置文件中设定的脚本对调度器的nginx 进程的监控 实验步骤: 基于上一篇LVS-DR架构来做,具体LVS-DR架构请参考上一篇 先设置主…

    2017-05-13
  • 运维自动化之系统安装

    自动化安装系统,cobbler的安装使用

    Linux干货 2018-01-15
  • Linux程序包管理

      Linux的各个release版本开发商,在发布各种应用程序,以及一些团体发布应用程序时,通常会根据发布的程序所适应的开发语言,使用环境,预设参数等,事先编译完成一个可以在相应平台上安装的程序包供使用者直接使用,该程序包含有安装前(preinstall)操作系统环境检测的脚本,程序包中所有文件的相关信息,程序预定义的配置参数文件,程序…

    Linux干货 2016-11-30
  • 一些不起眼但非常有用的 Vim 命令

    原文出处: xmodulo   译文出处:linux.cn – wangjiezhe   如果我的关于这个话题的最新帖子没有提醒到你的话,那我明确地说,我是一个 Vim 的粉丝。所以在你们中的某些人向我扔石头之前,我先向你们展示一系列“鲜为人知的 Vim 命令”。我的意思是,一些你可能以…

    Linux干货 2015-03-09
  • bash脚本基础

    一,概述 shell脚本 程序:指令+数据 程序编程风格: 过程式:以指令为中心,数据服务于指令 对象式:以数据为中心,指令服务于数据 shell程序:提供了编程能力,解释执行 编程逻辑处理方式: 顺序执行 循环执行 选择执行 shell编程:过程式、解释执行 编程语言的基本结构: 数据存储:变量、数组 表达式: a + b 语句:if shell脚本是包含…

    Linux干货 2016-08-19
  • N21天天第十二周课程练习

    1、请描述一次完整的http请求处理过程; 1、建立TCP连接 2、Web浏览器向Web服务器发送请求 3、Web浏览器发送请求头信息   建立连接后,客户机发送一个请求给服务器,请求方式的格式为:统一资源标识符(URL)、协议版本号,后边是MIME   信息包括请求修饰符、客户机信息和可能的内容 4、Web服务器应答…

    Linux干货 2016-10-31