python模塊的打包setuptools


樣例代碼:
新建test.py文件,內容如下:

[python]  view plain  copy
 
  1. print "show me"  


新建一個setup.py編譯文件,內容如下:

[python]  view plain  copy
 
  1. from distutils.core import setup  
  2. setup(name='Myblog',  #打包后的包文件名  
  3.       version='1.0',    
  4.       description='My Blog Distribution Utilities',    
  5.       author='Liu tiansi',    
  6.       author_email='liutiansi@gmail.com',    
  7.       url='http://blog.liuts.com',    
  8.       py_modules=['test'],   #與前面的新建文件名一致  
  9. )    


運行如下命令:
>>python setup.py sdist   #打包后的格式為tar.gz/zip

運行結果:
當前目錄下新增一個dist目錄,里面會有一個同name值相同的文件包。Windows下時zip包,linux下是tar.gz包。

安裝並測試:
解壓剛打包好的文件,運行如下命令進行安裝:
python setup.py install
進入python解釋器環境,運行如下命令:
import test
如果成功打印出show me字樣則表示成功

卸載:
python setup.py uninstall

setup函數各參數詳解:
>>python setup.py --help
  --name              包名稱
  --version (-V)      包版本
  --author            程序的作者
  --author_email      程序的作者的郵箱地址
  --maintainer        維護者
  --maintainer_email  維護者的郵箱地址
  --url               程序的官網地址
  --license           程序的授權信息
  --description       程序的簡單描述
  --long_description  程序的詳細描述
  --platforms         程序適用的軟件平台列表
  --classifiers       程序的所屬分類列表
  --keywords          程序的關鍵字列表
  --packages  需要打包的目錄列表
  --py_modules  需要打包的python文件列表
  --download_url  程序的下載地址
  --cmdclass  
  --data_files  打包時需要打包的數據文件,如圖片,配置文件等
  --scripts  安裝時需要執行的腳步列表

setup.py打包命令各參數詳解:
>>python setup.py --help-commands
  --python setup.py build     # 僅編譯不安裝
  --python setup.py install    #安裝到python安裝目錄的lib下
  --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 bdist --formats=zip  等價於  python setup.py sdist


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM