標籤:
轉自: 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 模組安裝包 製作