distutils、distutils2
distutils是 python 標准庫的一部分,2000年發布。使用它能夠進行 python 模塊的 安裝 和 發布。
distutils2 被設計為 distutils 的替代品,后來這個計划停滯了。
setuptools、easy_install、ez_setup.py
setuptools
是一個為了增強 distutils 而開發的集合,2004年發布。它包含了 easy_install 這個工具。
ez_setup.py
是 setuptools 的安裝工具。ez 是 easy 的縮寫。
使用方式:
- 從 PyPI 上安裝一個包:
easy_install requests
- 從網絡文件安裝(下載並安裝):
easy_install http://path/to/MyPackage-1.2.3.tgz
- 從一個本地 .egg 格式文件安裝:
easy_install /path/to/MyPackage-1.2.3.egg
distribute 是 setuptools 的一個分支版本,后來又合並到了setuptools。
pip
pip 是目前 python 包管理的事實標准,2008年發布。它被用作 easy_install 的替代品,但是它仍有大量的功能建立在 setuptools 組件之上。
pip 希望不再使用 Eggs 格式(雖然它支持 Eggs),而更希望采用“源碼發行版”(使用 python setup.py sdist 創建)。
pip使用方式:
- pip 可以利用
requirments.txt
來實現依賴的安裝。 - 支持 git/svn/hg 等流行的 VCS 系統,可以直接從 gz 或者 zip 壓縮包安裝,支持搜索包,以及指定服務器安裝等等功能。
- pip 提供了一個 wheel 子命令來安裝 wheel 包。需要先安裝 wheel 模塊。
setup.py vs requirements.txt ¶
- Python庫:那些被開發並且為了其他人來使用而發布的東西,你可以在 PyPI 找到很多Python庫。為了更好的推廣和傳播 自己,Python庫會包含很多的信息,比如它的名字,版本號,依賴等等。而
setup.py
就是用來提供這些信息的。但是,並沒有規定你可以從哪里獲取這些依賴庫。 - Python應用:指你所要部署的一些東西,這是區別於我們之前所講的Python庫的。一個應用經常會有很多依賴,或許會很復雜。這些依賴里很多沒有一個名字,或者沒有我們說所的那些信息。這便反映了 pip 的requirements文件所做的事情了。每個依賴都標明了准確的版本號,一般一個Python庫對依賴的版本比較寬松,而一個應用則會依賴比較具體的版本號。雖然也許跑其他 版本的 requests 並不會出錯,但是我們在本地測試順利后,我們就會希望在線上也跑相同的版本。執行
pip install -r requirements.txt
來安裝。
setup.py
from setuptools import setup
setup(
name="MyLibrary",
version="1.0",
install_requires=[
"requests",
"bcrypt",
],
# ...
)
requirements.txt
# This is an implicit value, here for clarity
--index https://pypi.python.org/simple/
MyPackage==1.0
requests==1.2.0
bcrypt==1.0.2
從抽象到具體
上面這個requirements.txt文件的頭部有一個 --index https://pypi.python.org/simple/
,一般如果你不用聲明這項,除非你使用的不是PyPI
。然而它卻是 requirements.txt
的一個重要部分, 這一行把一個抽象的依賴聲明 requests==1.2.0
轉變為一個具體的依賴聲明 requests 1.2.0 from pypi.python.org/simple/
在 setup.py
中,也存在一個 install_requires
表來指定依賴的安裝。這一功能除去了依賴的抽象特性,直接把依賴的獲取url標在了setup.py里。Link
from setuptools import setup
setup(
# ...
dependency_links = [
"http://packages.example.com/snapshots/",
"http://example2.com/p/bar-1.0.tar.gz",
],
)
wheel
wheel 本質上是一個 zip 包格式,它使用 .whl 擴展名,用於 python 模塊的安裝,它的出現是為了替代 Eggs。
wheel 還提供了一個 bdist_wheel
作為 setuptools 的擴展命令,這個命令可以用來生成 wheel 包。
pip 提供了一個 wheel 子命令來安裝 wheel 包。
setup.cfg
可以用來定義 wheel 打包時候的相關信息。
Python Wheels 網站展示了使用 Wheels 發行的 python 模塊在 PyPI 上的占有率。
.whl文件下載:http://www.lfd.uci.edu/~gohlke/pythonlibs/
總結
安裝
- Use pip to install Python packages from PyPI. Depending how pip is installed, you may need to also install wheel to get the benefit of wheel caching.
- Use virtualenv, or pyvenv to isolate application specific dependencies from a shared Python installation.
- If you’re looking for management of fully integrated cross-platform software stacks, consider buildout (primarily focused on the web development community) or Hashdist, or conda (both primarily focused on the scientific community).
打包
- Use
setuptools
to define projects and create Source Distributions. - Use the
bdist_wheel
setuptools extension available from the wheel project to create wheels. This is especially beneficial, if your project contains binary extensions. - Use
twine
for uploading distributions to PyPI.
第三方庫安裝路徑
Debian系的特殊路徑:Link
dist-packages instead of site-packages. Third party Python software installed from Debian packages goes into dist-packages, not site-packages. This is to reduce conflict between the system Python, and any from-source Python build you might install manually.
就是說從源代碼手動安裝,將使用site-packages
目錄。第三方python軟件安裝到dist-packages
目錄,這是為了減少與操作系統版本的python的沖突,因為Debian系統的許多工具都依賴與系統版本的python。
查找 Python 安裝路徑
>>> from distutils.sysconfig import get_python_lib
>>> print(get_python_lib())