(轉)python 模組安裝包 製作

來源:互聯網
上載者:User

標籤:

轉自: http://testerhome.com/topics/539

用過python的同學對於python setup.py install肯定不會陌生。那麼我們自己如果封裝了很多的方法怎麼很好的儲存或者開源呢?模組安裝包的製作是必不可少的。

假設我們的模組叫做monkey。那麼首先我們建立一個monkey.py。添加如下代碼:

# -*- coding: utf-8 -*-  class MyClass():    def __init__(self):        self.url = "http://www.testerhome.com"    def printurl(self):        print self.url

然後建立一個setup.py,添加如下代碼:

# -*- coding: utf-8 -*-  from distutils.core import setupsetup(name=‘test‘,    version=‘1.0‘,    description=‘MonkeyTest make first setup moudle‘,    author=‘MonkeyTest‘,    author_email=‘[email protected]‘,    url=‘http://www.testerhome.com‘,    py_modules=[‘monkey‘],    )

更多參數參考:

將兩個python檔案放入一個檔案夾,執行python setup.py sdist,然後會出現一個test-1.0.tar.gz的壓縮包,顯示如下log

running sdistrunning checkwarning: sdist: manifest template ‘MANIFEST.in‘ does not exist (using default file list)warning: sdist: standard file not found: should have one of README, README.txtwriting manifest file ‘MANIFEST‘creating test-1.0making hard links in test-1.0...hard linking monkey.py -> test-1.0hard linking setup.py -> test-1.0Creating tar archiveremoving ‘test-1.0‘ (and everything under it)

解壓縮之後可以看到檔案夾中有一個setup.py,這個就是我們可以install的py檔案。安裝之後我們可以嘗試

>>> from monkey import MyClass>>> app=MyClass()>>> app.printurl()http://www.testerhome.com

(轉)python 模組安裝包 製作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.