Python3 - 如何正确的使用pip

Python3 - 如何正确的使用pip

Python 有自己的包管理器,通过 pip 命令管理。本文介绍 pip 使用,遇到的问题以及解决方案。我们使用的 python 版本为 3.7, 对应的 pip 命令为 pip3。此外会介绍另一个命令 pipreqs,生成局部的安装包列表。

pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes.

pip 基本使用(增删改查)

1. 安装依赖

以安装 requests 和 beautifulsoup4 为例

1
2
pip3 install requests beautifulsoup4

2. 卸载安装包

1
pip3 uninstall requests beautifulsoup4

3. 显示包内文件

1
pip3 show --files requests

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(base) $ pip3 show --files 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: /usr/local/lib/python3.7/site-packages
Requires: chardet, urllib3, certifi, idna
Required-by: yarg, cos-python-sdk-v5
Files:
  requests-2.22.0.dist-info/INSTALLER
  requests-2.22.0.dist-info/LICENSE
  requests-2.22.0.dist-info/METADATA
  requests-2.22.0.dist-info/RECORD
  requests-2.22.0.dist-info/WHEEL
  requests-2.22.0.dist-info/top_level.txt
  ... ...

3. 列出已经安装的包

1
pip3 list

输出

1
2
3
4
5
6
7
8
9
10
base) bogon:mediumcn zjm$ pip3 list
Package                  Version
------------------------ ---------
asn1crypto               1.1.0
astroid                  2.3.3
autopep8                 1.5
beautifulsoup4           4.6.0
bz2file                  0.98
cachetools               3.1.1
... ...

4. 显示已经安装包的信息

显示 bs4 的安装包信息

1
pip3 show beautifulsoup4

输出

1
2
3
4
5
6
7
8
9
10
11
(base) $ pip3 show beautifulsoup4
Name: beautifulsoup4
Version: 4.6.0
Summary: Screen-scraping library
Home-page: http://www.crummy.com/software/BeautifulSoup/bs4/
Author: Leonard Richardson
Author-email: leonardr@segfault.org
License: MIT
Location: /usr/local/lib/python3.7/site-packages
Requires:
Required-by:

5. 显示已经过期的安装包

这个时间可能稍微又点久

1
pip3 list --outdated

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
(base) pip3 list --outdated
Package                  Version   Latest     Type
------------------------ --------- ---------- -----
asn1crypto               1.1.0     1.3.0      wheel
beautifulsoup4           4.6.0     4.8.2      wheel
cachetools               3.1.1     4.0.0      wheel
certifi                  2019.9.11 2019.11.28 wheel
cffi                     1.13.0    1.14.0     wheel
cryptography             2.7       2.8        wheel
google-api-python-client 1.7.11    1.7.12     wheel
google-auth              1.6.3     1.11.2     wheel
httplib2                 0.14.0    0.17.0
... ...

6. 升级安装包

1
pip3 install --upgrade beautifulsoup4

输出

1
2
3
4
5
6
7
8
9
10
11
(base) $ sudo  pip3 install --upgrade beautifulsoup4

Collecting beautifulsoup4
  Downloading https://files.pythonhosted.org/packages/cb/a1/c698cf319e9cfed6b17376281bd0efc6bfc8465698f54170ef60a485ab5d/beautifulsoup4-4.8.2-py3-none-any.whl (106kB)
     |████████████████████████████████| 112kB 94kB/s
Requirement already satisfied, skipping upgrade: soupsieve>=1.2 in /usr/local/lib/python3.7/site-packages (from beautifulsoup4) (2.0)
Installing collected packages: beautifulsoup4
  Found existing installation: beautifulsoup4 4.6.0
    Uninstalling beautifulsoup4-4.6.0:
      Successfully uninstalled beautifulsoup4-4.6.0
Successfully installed beautifulsoup4-4.8.2

7. 生成安装包列表

可以通过 freeze 命令完成,这样会把所有的安装包都存入到 requirements.txt

1
pip3 freeze > requirements.txt

但有时仅仅需要将当前工程依赖的安装包存储到 requirements.txt,该怎么做呢? 这里介绍一个新的命令 pipreqs

1
pip3 install pipreqs

通过下面命令,将自动生成当前库所以来的 pip 包,并覆盖到 requirements.txt

1
pipreqs . --encoding=utf8 --force

8. 通过 requirements.txt 安装依赖

1
pip3 install -r requirements.txt

以上在 macOS python3.7 下测试通过。

Rating: