distutils是 python 标准库的一部分,2000年发布。使用它可以进行 python 模块的 安装 和 发布。html
distutils2 被设计为 distutils 的替代品,后来这个计划停滞了。python
setuptools
是一个为了加强 distutils 而开发的集合,2004年发布。它包含了 easy_install 这个工具。git
ez_setup.py
是 setuptools 的安装工具。ez 是 easy 的缩写。web
使用方式:服务器
easy_install requests
easy_install http://path/to/MyPackage-1.2.3.tgz
easy_install /path/to/MyPackage-1.2.3.egg
distribute 是 setuptools 的一个分支版本,后来又合并到了setuptools。网络
pip 是目前 python 包管理的事实标准,2008年发布。它被用做 easy_install 的替代品,可是它仍有大量的功能创建在 setuptools 组件之上。app
pip 但愿再也不使用 Eggs 格式(虽然它支持 Eggs),而更但愿采用“源码发行版”(使用 python setup.py sdist 建立)。ide
pip使用方式:svn
requirments.txt
来实现依赖的安装。setup.py
就是用来提供这些信息的。可是,并无规定你能够从哪里获取这些依赖库。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 本质上是一个 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/
安装
打包
setuptools
to define projects and create Source Distributions.bdist_wheel
setuptools extension available from the wheel project to create wheels. This is especially beneficial, if your project contains binary extensions.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。
>>> from distutils.sysconfig import get_python_lib >>> print(get_python_lib())