你真的了解pip的所有用法吗

楔子

Python之所以流行,很大一部分程度是因为它拥有丰富的第三方库,你想做任何事情都有现成的工具。而安装这些第三库,我们一般都会使用pip进行安装,它是官方推荐的包管理工具,那你知道pip具体都有哪些用法吗?下面我们就来罗列一下。

查询软件包

查询当前环境安装的所有软件包:pip list

C:\Users\satori>pip list
Package               Version
--------------------- ------------
absl-py               0.9.0
aggdraw               1.3.11
aiobotocore           1.1.1
aiofiles              0.5.0
aiohttp               3.6.2
aiohttp-jinja2        1.2.0
aioitertools          0.7.0
aiopg                 1.0.0
amqp                  2.6.0
appdirs               1.4.3
arrow                 0.16.0
asgiref               3.2.3
async-timeout         3.0.1
.......
.......
.......

查询 pypi 上名字包含某个字符串的包:pip search pkg

C:\Users\satori>pip search pandas
pandas-ext (0.5.1)                - Python Pandas extensions for pandas dataframes
pandas-tfrecords (0.1.4)          - Converter pandas to tfrecords & tfrecords to pandas
pandas-cacher (0.1.3)             - Pandas cacher
pandas-mapper (0.1.4)             - Pandas Mapper
pandas-sets (0.2.1)               - Pandas - Sets:  Set-oriented Operations in Pandas
sklearn-pandas (2.0.2)            - Pandas integration with sklearn
xtalpi-pandas (0.1.5)             - XtalPi Pandas CMD
pandas-extensions (0.1)           - Extensions to the pandas package.
pandas-msgpack (0.1.4)            - Pandas interface to msgpack
pandas-path (0.1.2)               - Pathlib functionality for pandas.
pandas-multiprocessing (0.2.1)    - Pandas Multiprocessing Support
pandas-redistrict (0.0.3)         - Redistricting of pandas dataframes
pandas-should (0.1.0)             - pandas extension for asserting
prometheus-pandas (0.1.1.post1)   - Pandas integration for Prometheus
pandas-parallel (0.1.7)           - pandas apply parallel
.......
.......
.......

查询当前环境中可升级的包:pip list --outdated

C:\Users\satori>pip list --outdated
Package                Version    Latest     Type
---------------------- ---------- ---------- -----
aiofiles               0.5.0      0.6.0      wheel
aiohttp                3.6.2      3.7.3      wheel
aiopg                  1.0.0      1.1.0      wheel
alembic                1.4.2      1.4.3      wheel
amqp                   2.6.0      5.0.2      wheel
apache-airflow         1.10.11    2.0.0      wheel
apispec                1.3.3      4.0.0      wheel
appdirs                1.4.3      1.4.4      wheel
argcomplete            1.12.0     1.12.2     wheel
arrow                  0.15.6     0.17.0     wheel
asyncpg                0.20.1     0.21.0     wheel
attrs                  19.3.0     20.3.0     wheel
.......
.......
.......

查询一个包的详细内容:pip show pkg

C:\Users\satori>pip show requests
Name: requests
Version: 2.22.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: c:\python38\lib\site-packages
Requires: chardet, certifi, urllib3, idna
Required-by: twine, requests-toolbelt, yarg, requests-html, pyodps, pymars, oslo.config, infi.clickhouse-orm, apache-airflow

下载软件包

注意这里是下载,因为很多时候我们需要在内网进行安装,但是内网没有办法访问外网,这是一件很让人头疼的事情。这个时候我们就可以在有网的环境通过 pip 将安装包只下载到本地,也就是下载完毕之后不安装。

pip download -d 目录 包名

会将 whl 文件下载到指定的目录中,当然下载的还包含了当前包所依赖的其它包的 whl 文件。

C:\Users\satori>pip download -d c:\Users\satori\Desktop\pkgs fastapi

当然除了下载指定的包之外,还可以根据 requirement.txt 文件进行下载。

pip download -d 目录 -r requirement.txt

通过这种方式我们就可以轻松地下载一些库的 whl 文件,正如 pip install 安装的时候会自动解决依赖,pip download 下载也会自动下载相应的依赖。假设,此时我们将下载之后 whl 拷贝到内网服务器了,那么如何安装呢?

pip install --no-index --find-links=目录 包

当然使用 pip install 安装的时候,默认是包含下载这一步的,只不过它把下载和安装合在一起了。而我们已经下载好了,所以通过 --find-links 告诉 pip 直接去指定目录安装即可,不用先到 pypi 上进行下载。

C:\Users\satori>pip install --no-index --find-links=C:\Users\satori\Desktop\pkgs fastapi
Looking in links: c:\Users\satori\Desktop\pkgs

我们看到了 Looking in links: c:\Users\satori\Desktop\pkgs,表示当前是在我们指定的目录中进行查找的。

当然我们在安装的时候也可以指定 requirement.txt,即:pip install --no-index --find-links=目录 -r requirement.txt。

除此之外,我们也可以根据当前已有的环境,来构建生成相应的 whl 文件。

pip wheel --wheel-dir=目录 包名

我们可以将当前 Python 中已经存在的包打包成 whl 文件,当然除了指定包名之外,也可以换成 requirement.txt。

安装软件包

使用 pip install 可以很方便地从 pypi 上搜索下载并安装 python 包,但这是安装包的基本格式,我们还可以为其添加更多参数来实现不同的效果。

1. 只从本地安装,而不从 pypi 安装

  • pip install --no-index --find-links=目录 pkg, 之前说过的, 当然使用这种方式的前提是你得保证已经将安装包下载到指定目录下
  • pip install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com pkg, 通过 -i 指定别的源, 但由于不是https, 所以需要加上--trusted-host表示信任该网址

2. 安装指定版本

  • pip install pkg==2.1.2, 所安装的包的版本为 2.1.2
  • pip install pkg>=2.1.2, 所安装的包的版本大于等于 2.1.2
  • pip install pkg<=2.1.2, 所安装的包的版本小于等于 2.1.2

还可以管理 / 控制整个 python 环境的包版本

  • pip freeze > requirements.txt, 导出当前环境中的所有包
  • pip install -r requirements.txt, 从依赖包列表中安装, 这个我们也说过了, 可以是一个包, 也可以是一个 requirement.txt
  • pip install -c constraints.txt, 确保当前环境软件包的版本(并不确保安装)

3. 限制不使用二进制包安装

由于默认情况下,whl 包的平台是运行 pip download 命令的平台,所以可能出现平台不适配的情况。比如在 MacOS 系统下得到的 pymongo-2.8-cp27-none-macosx_10_10_intel.whl 就不能在 linux_x86_64 中安装,使用下面这条命令下载的是 tar.gz 的包,可以直接使用 pip install 安装。相比 whl 包,这种包在安装时会进行编译,所以花费的时间会长一些。

  • pip download --no-binary=:all: pkg, 下载非二进制的包
  • pip install pkg --no-binary, 安装非二进制的包

4. 指定代理服务器安装

当我们无法连接公网时,除了上面那种先将安装包下载下来之外,还可以使用代理。因为有的内网环境虽然和外界是隔离的,但两者之间的路并没有完全堵死,可以通过连接到指定的代理来建立一座桥。

pip install --proxy http[s]://[user:password@]ip:port 包名

但每次安装包就发输入长长的参数,未免有些麻烦,为此你可以将其写入配置文件中:$HOME/.config/pip/pip.conf。但是对于这个路径,不同的操作系统,路径各不相同:

# Linux/Unix:
/etc/pip.conf
~/.pip/pip.conf
~/.config/pip/pip.conf
 
# Mac OSX:
~/Library/Application Support/pip/pip.conf
~/.pip/pip.conf
/Library/Application Support/pip/pip.conf
 
# Windows:
%APPDATA%\pip\pip.ini
%HOME%\pip\pip.ini
C:\Documents and Settings\All Users\Application Data\PyPA\pip\pip.conf (Windows XP)
C:\ProgramData\PyPA\pip\pip.conf (Windows 7及以后)

若在你的机子上没有此文件,则自行创建即可:

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
 
# 替换出自己的代理地址,格式为 http://[user:passwd@]ip:port
proxy=http://xxx.xxx.xxx.xxx:8080
 
[install]
# 信任阿里云的镜像源,否则会有警告
trusted-host=mirrors.aliyun.com

卸载软件包

就一条命令,不再赘述:pip uninstall pkg

升级软件包

对现有的 python 包进行升级,其本质上也是先从 pypi 上下载最新版本的包,再对其进行安装,安装之后再删除老版本的包。所以升级也是使用 pip install,只不过要加一个参数 --upgrade

pip install --upgrade pkg

升级的时候,其实还有一个不怎么用到的选项 --upgrade-strategy,它是用来指定升级策略。它的可选项只有两个:

  • eager: 升级全部依赖包
  • only-if-need: 只有当旧版本不能适配新的父依赖包时, 才会升级

在 pip 10.0 版本之后,这个选项的默认值是 only-if-need,因此如下两种写法是一互致的

pip install --upgrade pkg1
pip install --upgrade pkg1 --upgrade-strategy only-if-need

以上便是 pip 的基本用法。

posted @ 2019-01-04 23:20  古明地盆  阅读(1064)  评论(0编辑  收藏  举报