设为首页 加入收藏

TOP

Python 包管理工具 pip 安装 和 使用
2018-12-13 18:35:38 】 浏览:16
Tags:Python 管理工具 pip 安装 使用

pip安装使用详解:http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa
python 包:https://pypi.python.org/pypi

在 python 中安装非自带 python 模块,有三种方式:

1.easy_install
2.pip
3.下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的目录后执行python setup.py install命令

安装 setuptools,用easy_install *.egg 命令来安装
一般 python setup.py install 安装后的模块,就能import了
安装 python 模块,建议使用 pypi

pip 类似 RedHat 里面的 yum,安装Python包非常方便。本节详细介绍 pip 的安装、以及使用方法。

推荐:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py


如果安装的是 python3,则 python3 默认安装的有 pip3 ,不需要再自己安装 pip3。
pip3 安装在 python3 路径下的 bin 目录里面

升级 pip3

通常情况下,你的电脑里如果安装了python2.x也同时安装了python3.x,那么应该就会有两个pip。一个是pip2,另一个是pip3。好吧,可能还有一个既没有2,也米有3的pip,一般情况下,pip==pip2。

有时候我们使用pip安装东西会提示我们pip的版本过低,建议我们升级,一般情况下我们用pip3的时候固然会想到用下面的命令来升级:pip3 install --upgrade pip3。但如果你这么做了,你会发现好像这并不是正确的姿势。实际上这是一个逻辑错误,看了正确答案你就会知道哪里出错了。

正确的升级 pip3 如下pip3 install --upgrade pip

使用 豆瓣源 安装:pip install robobrowser -i http://pypi.douban.com/simple/

1. pip下载安装

pip 下载

# wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate

或者 到 pypi 官网(https://pypi.python.org/pypi ) 直接下载源码安装

pip安装

# tar -xzvf pip-1.5.4.tar.gz
# cd pip-1.5.4
# python setup.py install

2. pip使用详解

pip 使用帮助

root@kali:~$ pip -h

Usage:
  pip <command> [options]

Commands:
  install                     安装包.
  download                    下载包.
  uninstall                   卸载包.
  freeze                      按照一定格式输出已安装包的列表.
  list                        列出已经安装的包.
  show                        显示所安装包的信息.
  check                       验证已安装的包所具有的兼容依赖关系.
  search                      通过 PyPI 搜索包.
  wheel                       Build wheels from your requirements.
  hash                        计算包文件的哈希值.
  completion                  用于命令完成的助手命令。
  help                        命令帮助.

General Options:
  -h, --help                  显示帮助.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  -v, --verbose               更多的输出,最多可以使用3次
  -V, --version               显示版本号并退出
  -q, --quiet                 安静模式,显示最少的输出。
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             使用代理。代理格式: [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             设置 socket 超时时间。(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.

如果想看 pip 中某个 命令的帮助,例如 pip list ,可以使用: pip list -h

root@kali:~$ pip list -h

Usage:
  pip list [options]

Description:
  List installed packages, including editables.

  Packages are listed in a case-insensitive sorted order.

List Options:
  -o, --outdated              List outdated packages
  -u, --uptodate              List uptodate packages
  -e, --editable              List editable projects.
  -l, --local                 If in a virtualenv that has global access, do not list globally-installed packages.
  --user                      Only output packages installed in user-site.
  --pre                       Include pre-release and development versions. By default, pip only finds stable versions.
  --format <list_format>      Select the output format among: legacy (default), columns, freeze or json.
  --not-required              List packages that are not dependencies of installed packages.

Package Index Options (including deprecated options):
  -i, --index-url <url>       Base URL of Python Package Index (default https://pypi.python.org/simple). This should
                              point to a repository compliant with PEP 503 (the simple repository API) or a local
                              directory laid out in the same format.
  --extra-index-url <url>     Extra URLs of package indexes to use in addition to --index-url. Should follow the same
                              rules as --index-url.
  --no-index                  Ignore package index (only looking at --find-links URLs instead).
  -f, --find-links <url>      If a url or path to an html file, then parse for links to archives. If a local path or
                              file:// url that's a directory, then look for archives in the directory listing.
  --process-dependency-links  Enable the processing of dependency links.

示例: pip list --outdata 查看需要更新的 python 包

使用 pip 安装包

# pip install SomePackage
  [...]
  Successfully installed SomePackage

使用 pip 查看已安装的包

# pip show --files SomePackage
 Name:SomePackage
 Version:1.0
 Location:/my/env/lib/pythonx.x/site-packages
 Files:
  ../somepackage/__init__.py
  [...]

使用 pip 检查哪些包需要更新

# pip list --outdated
  SomePackage (Current: 1.0 Latest: 2.0)

使用pip 升级包

# pip install --upgrade SomePackage
 [...]
 Foundexistinginstallation:SomePackage1.0
 UninstallingSomePackage:
   SuccessfullyuninstalledSomePackage
 Runningsetup.pyinstallforSomePackage
 SuccessfullyinstalledSomePackage

使用 pip 卸载包

$ pip uninstall SomePackage
  Uninstalling SomePackage:
    /my/env/lib/pythonx.x/site-packages/somepackage
  Proceed (y/n) y
  Successfully uninstalled SomePackage

3. pip使用实例

以 安装 redis 示例:

安装redis
# pip install redis


卸载redis
# pip uninstall redis
Uninstalling redis:
  /usr/lib/python2.6/site-packages/redis-2.9.1-py2.6.egg-info
.....省略一些内容....
Proceed (y/n) y
  Successfully uninstalled redis


查看待更新包
pip list --outdate

常见错误:ImportError No module named setuptools。说明没有安装setuptools 模块,直接下载安装即可

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python Exercise #32 下一篇网页抓取神器scrapy的安装搭建

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目