命令

安装 pip install

升级/更新

pip install pip —upgrade
pip install pip -U

下载 pip download

pip download
离线安装wheel文件,适用:无网络环境或公司内网
下载wheel文件
pip download XXX # 目标库文件及依赖包
示例:pip install requests

  1. ~ pip download requests
  2. Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
  3. Collecting requests
  4. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/92/96/144f70b972a9c0eabbd4391ef93ccd49d0f2747f4f6a2a2738e99e5adc65/requests-2.26.0-py2.py3-none-any.whl (62 kB)
  5. |████████████████████████████████| 62 kB 554 kB/s
  6. Collecting urllib3<1.27,>=1.21.1
  7. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5f/64/43575537846896abac0b15c3e5ac678d787a4021e906703f1766bfb8ea11/urllib3-1.26.6-py2.py3-none-any.whl (138 kB)
  8. |████████████████████████████████| 138 kB 1.4 MB/s
  9. Collecting certifi>=2017.4.17
  10. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/05/1b/0a0dece0e8aa492a6ec9e4ad2fe366b511558cdc73fd3abc82ba7348e875/certifi-2021.5.30-py2.py3-none-any.whl (145 kB)
  11. |████████████████████████████████| 145 kB 4.6 MB/s
  12. Collecting idna<4,>=2.5
  13. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d7/77/ff688d1504cdc4db2a938e2b7b9adee5dd52e34efbd2431051efc9984de9/idna-3.2-py3-none-any.whl (59 kB)
  14. |████████████████████████████████| 59 kB 5.8 MB/s
  15. Collecting charset-normalizer~=2.0.0
  16. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c4/1d/e6ce112f7237fc746e632e1cbdc24890cad95505c6cd4b711f4fd17f4735/charset_normalizer-2.0.3-py3-none-any.whl (35 kB)
  17. Saved ./requests-2.26.0-py2.py3-none-any.whl
  18. Saved ./certifi-2021.5.30-py2.py3-none-any.whl
  19. Saved ./charset_normalizer-2.0.3-py3-none-any.whl
  20. Saved ./idna-3.2-py3-none-any.whl
  21. Saved ./urllib3-1.26.6-py2.py3-none-any.whl
  22. Successfully downloaded requests certifi charset-normalizer idna urllib3
  23. ~ ll *.whl
  24. -rw-r--r-- 1 yuliming staff 142K 7 23 14:34 certifi-2021.5.30-py2.py3-none-any.whl
  25. -rw-r--r-- 1 yuliming staff 35K 7 23 14:34 charset_normalizer-2.0.3-py3-none-any.whl
  26. -rw-r--r-- 1 yuliming staff 58K 7 23 14:34 idna-3.2-py3-none-any.whl
  27. -rw-r--r-- 1 yuliming staff 61K 7 23 14:34 requests-2.26.0-py2.py3-none-any.whl
  28. -rw-r--r-- 1 yuliming staff 135K 7 23 14:34 urllib3-1.26.6-py2.py3-none-any.whl

单独wheel文件下载(不推荐,需一个个下载依赖库的wheel)
Python Extension Packages for Windows - Christoph Gohlke (uci.edu)
PyPI·The Python Package Index
安装wheel文件
pip install .whl
python3 -m pip install
.whl —user

查看 pip list

查看已安装的库列表pip list

  1. ~ pip list
  2. Package Version
  3. --------------------------------- ---------
  4. backports.entry-points-selectable 1.1.0
  5. certifi 2021.5.30
  6. chardet 4.0.0
  7. cx-Oracle 8.2.1
  8. distlib 0.3.2
  9. filelock 3.0.12
  10. idna 2.10
  11. numpy 1.20.3
  12. pandas 1.2.4
  13. pip 21.1.3
  14. pipenv 2021.5.29
  15. platformdirs 2.0.2
  16. python-dateutil 2.8.1
  17. pytz 2021.1
  18. requests 2.25.1
  19. setuptools 56.0.0
  20. six 1.16.0
  21. urllib3 1.26.5
  22. virtualenv 20.6.0
  23. virtualenv-clone 0.5.5

配置 pip config

查看配置
pip config list
pip config list -v

  1. ~ pip config list
  2. global.default-timeout='120'
  3. global.disable-pip-version-check='true'
  4. global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
  5. global.trusted-host='mirrors.aliyun.com'
  6. list.format='columns'
  7. ~ pip config list -v
  8. For variant 'global', will try loading '/Library/Application Support/pip/pip.conf'
  9. For variant 'user', will try loading '/Users/yuliming/.pip/pip.conf'
  10. For variant 'user', will try loading '/Users/yuliming/.config/pip/pip.conf'
  11. For variant 'site', will try loading '/Library/Frameworks/Python.framework/Versions/3.9/pip.conf'
  12. global.default-timeout='120'
  13. global.disable-pip-version-check='true'
  14. global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
  15. global.trusted-host='mirrors.aliyun.com'
  16. list.format='columns'

配置

配置文件
可能存在的配置文件信息路径
pip config list -v
-v 显示更多信息

  1. ~ pip config list -v
  2. For variant 'global', will try loading '/Library/Application Support/pip/pip.conf'
  3. For variant 'user', will try loading '/Users/yuliming/.pip/pip.conf'
  4. For variant 'user', will try loading '/Users/yuliming/.config/pip/pip.conf'
  5. For variant 'site', will try loading '/Library/Frameworks/Python.framework/Versions/3.9/pip.conf'

写入配置文件
pip config set KEY VALUE

  1. pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  2. pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn
  3. pip config set global.disable-pip-version-check true
  4. pip config set global.default-timeout 60
  5. pip config set list.format columns

结果
vim ~/.config/pip/pip.conf

  1. [global] # 全局配置,可单独配置download、install等
  2. index-url = https://pypi.tuna.tsinghua.edu.cn/simple # 镜像源
  3. trusted-host = mirrors.aliyun.com # 信任,免去SSL验证
  4. disable-pip-version-check = true # 不检查pip版本
  5. default-timeout = 60 # 下载超时时间
  6. [list]
  7. format = columns # 设置pip list输出格式

镜像源:

默认:https://pypi.org/simple 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/ 华中科技大学:http://pypi.hustunique.com/ 山东理工大学:http://pypi.sdutlinux.org/ 豆瓣:http://pypi.douban.com/simple/

pip freeze(requirements.txt)

查看已安装的库列表
pip freeze

  1. ~ pip freeze
  2. backports.entry-points-selectable==1.1.0
  3. certifi==2021.5.30
  4. chardet==4.0.0
  5. cx-Oracle==8.2.1
  6. distlib==0.3.2
  7. filelock==3.0.12
  8. idna==2.10
  9. numpy==1.20.3
  10. pandas==1.2.4
  11. pipenv==2021.5.29
  12. platformdirs==2.0.2
  13. python-dateutil==2.8.1
  14. pytz==2021.1
  15. requests==2.25.1
  16. six==1.16.0
  17. urllib3==1.26.5
  18. virtualenv==20.6.0
  19. virtualenv-clone==0.5.5

生成requirements.txt
pip freeze > requirements.txt

  1. ~ pip freeze > requirements.txt
  2. ~ cat requirements.txt
  3. backports.entry-points-selectable==1.1.0
  4. certifi==2021.5.30
  5. chardet==4.0.0
  6. cx-Oracle==8.2.1
  7. distlib==0.3.2
  8. filelock==3.0.12
  9. idna==2.10
  10. numpy==1.20.3
  11. pandas==1.2.4
  12. pipenv==2021.5.29
  13. platformdirs==2.0.2
  14. python-dateutil==2.8.1
  15. pytz==2021.1
  16. requests==2.25.1
  17. six==1.16.0
  18. urllib3==1.26.5
  19. virtualenv==20.6.0
  20. virtualenv-clone==0.5.5

从requirements.txt安装库
pip install -r requirements.txt

  1. ~ pip install -r requirements.txt
  2. Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
  3. Requirement already satisfied: backports.entry-points-selectable==1.1.0 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from -r requirements.txt (line 1)) (1.1.0)
  4. Requirement already satisfied: certifi==2021.5.30 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from -r requirements.txt (line 2)) (2021.5.30)

帮助pip help

pip -h

问题

一. 如何解决pip install下载速度慢、易报错

  1. 增加超时时间

    pip install XXX —default-timeout=1000

  2. 更换镜像源

    pip install XXX -i http://pypi.douban.com/simple/--trusted-host pypi.douban.com