Python包管理工具简介

概述

在Python环境中已经有很多成熟的包,可以通过安装这些包来扩展我们的程序。

例如,很多时候Python开发人员都会去PyPI网站去查找自己想要使用的包,然后进行安装。PyPI ( Python Package Index)是获得第三方 Python 软件包以补充标准库的一个站点。

在安装Python包的过程中,经常涉及到distutils、setuptools、distribute、setup.py、easy_install、easy_install和pip等等。

突然对Python的包管理凌乱了,这些工具关系是什么,应该怎么去选择使用?

下面就简单介绍下Python包管理相关的一些内容。

Python包管理工具

这么多的Python包管理工具,刚开始接触Python时会有点不知如何选择。其实,当查过Python的官方文档之后,慢慢就理清了这些工具之间的关系了,也就没有这么混乱了。

下面就分别对Python中的包管理工具进行简单的介绍。

distutils

distutils 是 python 标准库的一部分,这个库的目的是为开发者提供一种方便的打包方式, 同时为使用者提供方便的安装方式。

我们经常使用的setup.py就是基于distutils实现的,然后通过setup.py就可以进行打包或者安装了。

setuptools 和 distribute

setuptools 是对 distutils 的增强,尤其是引入了包依赖管理。我们可以通过ez_setup.py来安装setuptools。

至于distribute,它是setuptools的一个分支版本。分支的原因是有一部分开发者认为 setuptools 开发太慢。但现在,distribute 又合并回了 setuptools 中,所以可以认为它们是同一个东西。

前面看到setup.py可以创建一个压缩包,而setuptools使用了一种新的文件格式(.egg),可以为Python包创建 egg文件。setuptools 可以识别.egg文件,并解析、安装它

easy_install

当安装好setuptools/distribute之后,我们就可以直接使用easy_install这个工具了:

  • 从PyPI上安装一个包:当使用 easy_install package 命令后,easy_install 可以自动从 PyPI 上下载相关的包,并完成安装,升级

  • 下载一个包安装:通过 easy_install package.tgz 命令可以安装一个已经下载的包

  • 安装egg文件:通过 easy_install package.egg 可以安装一个egg格式的文件

通过 easy_install –help 命令可以获取该命令相关的帮助提示:

C:\Users\john>easy_install --help

Global options:
  --verbose (-v)  run verbosely (default)
  --quiet (-q)    run quietly (turns verbosity off)
  --dry-run (-n)  don't actually do anything
  --help (-h)     show detailed help message
  --no-user-cfg   ignore pydistutils.cfg in your home directory

Options for 'easy_install' command:
  --prefix                   installation prefix
  --zip-ok (-z)              install package as a zipfile
  --multi-version (-m)       make apps have to require() a version
  --upgrade (-U)             force upgrade (searches PyPI for latest versions)
  --install-dir (-d)         install package to DIR
  --script-dir (-s)          install scripts to DIR
  --exclude-scripts (-x)     Don't install scripts
  --always-copy (-a)         Copy all needed packages to install dir
  --index-url (-i)           base URL of Python Package Index
  --find-links (-f)          additional URL(s) to search for packages
  --build-directory (-b)     download/extract/build in DIR; keep the results
  --optimize (-O)            also compile with optimization: -O1 for "python -
                             O", -O2 for "python -OO", and -O0 to disable
                             [default: -O0]
  --record                   filename in which to record list of installed
                             files
  --always-unzip (-Z)        don't install as a zipfile, no matter what
  --site-dirs (-S)           list of directories where .pth files work
  --editable (-e)            Install specified packages in editable form
  --no-deps (-N)             don't install dependencies
  --allow-hosts (-H)         pattern(s) that hostnames must match
  --local-snapshots-ok (-l)  allow building eggs from local checkouts
  --version                  print version information and exit
  --no-find-links            Don't load find-links defined in packages being
                             installed
  --user                     install in user site-package
                             'C:\Users\john\AppData\Roaming\Python\Python37\si
                             te-packages'

usage: easy_install [options] requirement_or_url ...
   or: easy_install --help


C:\Users\john>^A

根据上面的分析,可以看到setuptools/distribute和easy_install之间的关系:

  • setuptools/distribute 都扩展了 distutils,提供了更多的功能

  • easy_install是基于setuptools/distribute的一个工具,方便了包的安装和省级

pip

pip是目前最流行的Python包管理工具,它被当作easy_install的替代品,但是仍有大量的功能建立在setuptools之上。

easy_install 有很多不足:安装事务是非原子操作,只支持 svn,没有提供卸载命令, 安装一系列包时需要写脚本。pip 解决了以上问题,已经成为新的事实标准。

pip的使用非常简单,并支持从任意能够通过 VCS 或浏览器访问到的地址安装 Python 包:

  • 安装: pip install SomePackage

  • 卸载: pip uninstall SomePackage

在大家使用Python中,推荐使用pip进行Python包管理,pip的安装和使用都比较方便。

pip安装
pip的安装有两种常用的方式:

  1. 下载get-pip.py文件,然后执行 python get-pip.py 进行安装(如果没有安装setuptools,那么get-pip.py会帮忙安装)

  2. 下载pip源码包,然后通过setup.py进行安装

pip常用命令

对于pip,最常用的肯定还是 pip –help ,通过帮助文档,就可以大概知道如何使用命令和参数。

C:\Users\john>pip --help

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependen
cies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring
                              environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be
                              used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be
                              used up to 3 times (corresponding to WARNING,
                              ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form
                              [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should
                              attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists:
                              (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort).
  --trusted-host <hostname>   Mark this host as trusted, even though it does
                              not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file
                              containing the private key and the certificate
                              in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine
                              whether a new version of pip is available for
                              download. Implied with --no-index.
  --no-color                  Suppress colored output

C:\Users\john>

总结

本文介绍了Python的各个包管理工具之间的关系,相信通过这篇文章的介绍,就不会再对distutils、setuptools、distribute、setup.py、easy_install、easy_install和pip等等名词感觉混乱了。

大致了解这些Python包管理工具后,大家应该也清楚了怎么选择和使用。

本文没有涉及如何制作并发布一个Python包,感兴趣的同学可以去Python官网上查查。


原文出处 http://www.cnblogs.com/wilber2013/ (略有改动)

猜你喜欢

转载自blog.csdn.net/guotianqing/article/details/80288643