- 官方文檔: Building and Distributing Packages with Setuptools
- 中文文檔: Python包管理工具setuptools詳解html
1.使用過程python
編輯: 先在項目主目錄下編輯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的模板python2.7
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. )
聲明依賴包的語法:ide
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.svn
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 -.ui
其它例子:url
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 spa
二.Python項目的打包、發佈和部署的經常使用方法比較.net
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目錄中
參考: