制作python模塊安裝包[原創]

 python的第三方模塊越來越豐富,涉及的領域也非常廣,如科學計算、圖片處理、web應用、GUI開發等。當然也可以將自己寫的模塊進行打包或發布。一簡單的方法是將你的類包直接copy到python的lib目錄,但此方式不便于管理與維護,存在多個python版本時會非?;靵y?,F介紹如何編寫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.www58058.com/1737

(0)
s19930811s19930811
上一篇 2015-03-27
下一篇 2015-03-27

相關推薦

  • Docker之~集群配置

    一、前言 Kubernetes 是Google開源的容器集群管理系統,基于Docker構建一個容器的調度服務,提供資源調度、均衡容災、服務注冊、動態擴縮容等功能套件,目前最新版本為0.6.2。 本文介紹如何基于Centos7.0構建Kubernetes平臺,在正式介紹之前,大家有必要先理解Kubernetes幾個核心概念及其承擔的功能。以下為Kubernet…

    2017-03-16
  • Linux程序包管理

    Linux程序包管理 RPM包基礎 RPM YUM 編譯安裝 概述 Linux上的程序包管理,是我們的必備技能,本文將從三個視角:rpm、yum、源碼編譯來淺析Linux的程序包管理。首先,我們來看下本文的主要內容: – 程序包的命名- RPM: 程序包管理器    安裝、卸載、升級、查詢…

    Linux干貨 2016-08-29
  • linux樹狀結構

    linux 目錄結構   /:根目錄,一般根目錄下只存放目錄,不要存放文件,/etc、/bin、/dev、/lib、/sbin應該和根目錄放置在一個分區中 /bin:/usr/bin:可執行二進制文件的目錄,如常用的命令ls、tar、mv、cat等。 /boot:放置linux系統啟動時用到的一些文件。/boot/vmlinuz為linux的內核文…

    Linux干貨 2016-08-05
  • vi編輯器

       vim編輯器是眾多文件編輯軟件中比較好的一個,也是使用最多 ,功能最強大的,提供了圖形化界面。    Vi:類unix系統中默認的文本編輯器    Vim:vi編輯器的增強版本,習慣上也稱vi。    作用:創建或修改文本文件,維護linux系統的各種配置文件 vim的使用…

    Linux干貨 2016-08-15
  • 配置邏輯卷·設定LVM快照·btrfs文件系統

    _ueditor_page_break_tag_ 配置邏輯卷,設定LVM快照,btrfs文件系統                 邏輯卷管理器( LVM)v允許對卷進行方便操作的抽象層,包括重新設定文…

    Linux干貨 2016-09-01
  • linux-系統自動化安裝

    一、安裝程序及配置 CentOS 系統安裝 系統啟動流程: bootloader–>kernel(initramfs)–>rootfs–>/sbin/init anaconda: 系統安裝程序 tui:  基于圖形庫curses 的文本 窗口 gui :圖形窗口 安裝程序啟動過程 MB…

    Linux干貨 2016-09-26
欧美性久久久久