[Python] 項目打包發布


一.setuptools

- 官方文檔: Building and Distributing Packages with Setuptools
- 中文文檔: Python包管理工具setuptools詳解

 

1.使用過程

編輯: 先在項目主目錄下編輯setup.py,
打包: python setup.py sdist 
安裝: sudo python setup.py install  (--record files.txt)
卸載: sudo cat files.txt | sudo  xargs rm -rf

 

2. setuptools工具的setup.py的模板

from setuptools import setup, find_packages
setup(
    name="HelloWorld",
    version="0.1",
    packages=find_packages(),
    scripts=['say_hello.py'],

    # Project uses reStructuredText, so ensure that the docutils get
    # installed or upgraded on the target machine
    install_requires=['docutils>=0.3'],

    package_data={
        # If any package contains *.txt or *.rst files, include them:
        '': ['*.txt', '*.rst'],
        # And include any *.msg files found in the 'hello' package, too:
        'hello': ['*.msg'],
    },

    # metadata for upload to PyPI
    author="Me",
    author_email="me@example.com",
    description="This is an Example Package",
    license="PSF",
    keywords="hello world example examples",
    url="http://example.com/HelloWorld/",   # project home page, if any

    # could also include long_description, download_url, classifiers, etc.
)

 

聲明依賴包的語法:

This syntax consists of a project’s PyPI name, optionally followed by a comma-separated list of “extras” in square brackets, optionally followed by a comma-separated list of version specifiers.

A version specifier is one of the operators <, >, <=, >=, == or !=, followed by a version identifier.

Tokens may be separated by whitespace, but any whitespace or nonstandard characters within a project name or version identifier must be replaced with -.

其它例子:

docutils >= 0.3

# comment lines and \ continuations are allowed in requirement strings
BazSpam ==1.1, ==1.2, ==1.3, ==1.4, ==1.5, \
==1.6, ==1.7 # and so are line-end comments

PEAK[FastCGI, reST]>=0.5a4

setuptools==0.5a7

詳情可參考官方說明:Declaring Dependencies 

 

二.Python項目的打包、發布和部署的常用方法比較

1. distutils - python自帶的基本安裝工具, 適用於非常簡單的應用場景使用, 不支持依賴包的安裝 
  通過distutils來打包,生成安裝包,安裝python包等工作,需要編寫名為setup.py python腳本文件。

2. setuptools - 針對 distutils 做了大量擴展, 尤其是加入了包依賴機制。不支持python3,安裝完setuptools后會有easy_install

3. distribute - 類似於setuptools,支持python3,安裝完distribute后會有easy_install。

4. easy_install - setuptools 和 distribute 自帶的安裝腳本, 也就是一旦setuptools或distribute安裝完畢, easy_install 也便可用了。

5. pip - 目標是取代easy_install。easy_install 有很多不足: 安裝事務是非原子操作, 只支持 svn, 沒有提供卸載命令, 安裝一系列包時需要寫腳本; pip 解決了以上問題, 已儼然成為新的事實標准, virtualenv 與它已經成為一對好搭檔;

6. distutils2 - setuptools 和 distribute 的誕生是因為 distutils 的不濟, 進而導致目前分化的狀況。它將成為 Python 3.3 的標准庫 packaging , 並在其它版本中以distutils2 的身份出現; 換句話說, 它和 pip 將聯手結束目前混亂的狀況。

7. virtualenv - 用來創建隔離的python環境,處理python環境的多版本和模塊依賴。

 

常識
1. sudo apt-get install 安裝的package存放在 /usr/lib/python2.7/dist-packages目錄中
2. pip 或者 easy_install安裝的package存放在/usr/local/lib/python2.7/dist-packages目錄中
3. 手動從源代碼安裝的package存放在site-packages目錄中

 

參考:

python項目打包發布總結

 


免責聲明!

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



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