命令
安装 pip install
升级/更新
pip install pip —upgrade
pip install pip -U
下载 pip download
pip download
离线安装wheel文件,适用:无网络环境或公司内网
下载wheel文件
pip download XXX # 目标库文件及依赖包
示例:pip install requests
➜ ~ pip download requests
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting requests
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/92/96/144f70b972a9c0eabbd4391ef93ccd49d0f2747f4f6a2a2738e99e5adc65/requests-2.26.0-py2.py3-none-any.whl (62 kB)
|████████████████████████████████| 62 kB 554 kB/s
Collecting urllib3<1.27,>=1.21.1
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5f/64/43575537846896abac0b15c3e5ac678d787a4021e906703f1766bfb8ea11/urllib3-1.26.6-py2.py3-none-any.whl (138 kB)
|████████████████████████████████| 138 kB 1.4 MB/s
Collecting certifi>=2017.4.17
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/05/1b/0a0dece0e8aa492a6ec9e4ad2fe366b511558cdc73fd3abc82ba7348e875/certifi-2021.5.30-py2.py3-none-any.whl (145 kB)
|████████████████████████████████| 145 kB 4.6 MB/s
Collecting idna<4,>=2.5
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d7/77/ff688d1504cdc4db2a938e2b7b9adee5dd52e34efbd2431051efc9984de9/idna-3.2-py3-none-any.whl (59 kB)
|████████████████████████████████| 59 kB 5.8 MB/s
Collecting charset-normalizer~=2.0.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c4/1d/e6ce112f7237fc746e632e1cbdc24890cad95505c6cd4b711f4fd17f4735/charset_normalizer-2.0.3-py3-none-any.whl (35 kB)
Saved ./requests-2.26.0-py2.py3-none-any.whl
Saved ./certifi-2021.5.30-py2.py3-none-any.whl
Saved ./charset_normalizer-2.0.3-py3-none-any.whl
Saved ./idna-3.2-py3-none-any.whl
Saved ./urllib3-1.26.6-py2.py3-none-any.whl
Successfully downloaded requests certifi charset-normalizer idna urllib3
➜ ~ ll *.whl
-rw-r--r-- 1 yuliming staff 142K 7 23 14:34 certifi-2021.5.30-py2.py3-none-any.whl
-rw-r--r-- 1 yuliming staff 35K 7 23 14:34 charset_normalizer-2.0.3-py3-none-any.whl
-rw-r--r-- 1 yuliming staff 58K 7 23 14:34 idna-3.2-py3-none-any.whl
-rw-r--r-- 1 yuliming staff 61K 7 23 14:34 requests-2.26.0-py2.py3-none-any.whl
-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
➜ ~ pip list
Package Version
--------------------------------- ---------
backports.entry-points-selectable 1.1.0
certifi 2021.5.30
chardet 4.0.0
cx-Oracle 8.2.1
distlib 0.3.2
filelock 3.0.12
idna 2.10
numpy 1.20.3
pandas 1.2.4
pip 21.1.3
pipenv 2021.5.29
platformdirs 2.0.2
python-dateutil 2.8.1
pytz 2021.1
requests 2.25.1
setuptools 56.0.0
six 1.16.0
urllib3 1.26.5
virtualenv 20.6.0
virtualenv-clone 0.5.5
配置 pip config
查看配置
pip config list
pip config list -v
➜ ~ pip config list
global.default-timeout='120'
global.disable-pip-version-check='true'
global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
global.trusted-host='mirrors.aliyun.com'
list.format='columns'
➜ ~ pip config list -v
For variant 'global', will try loading '/Library/Application Support/pip/pip.conf'
For variant 'user', will try loading '/Users/yuliming/.pip/pip.conf'
For variant 'user', will try loading '/Users/yuliming/.config/pip/pip.conf'
For variant 'site', will try loading '/Library/Frameworks/Python.framework/Versions/3.9/pip.conf'
global.default-timeout='120'
global.disable-pip-version-check='true'
global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
global.trusted-host='mirrors.aliyun.com'
list.format='columns'
配置
配置文件
可能存在的配置文件信息路径
pip config list -v
-v 显示更多信息
➜ ~ pip config list -v
For variant 'global', will try loading '/Library/Application Support/pip/pip.conf'
For variant 'user', will try loading '/Users/yuliming/.pip/pip.conf'
For variant 'user', will try loading '/Users/yuliming/.config/pip/pip.conf'
For variant 'site', will try loading '/Library/Frameworks/Python.framework/Versions/3.9/pip.conf'
写入配置文件
pip config set KEY VALUE
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn
pip config set global.disable-pip-version-check true
pip config set global.default-timeout 60
pip config set list.format columns
结果
vim ~/.config/pip/pip.conf
[global] # 全局配置,可单独配置download、install等
index-url = https://pypi.tuna.tsinghua.edu.cn/simple # 镜像源
trusted-host = mirrors.aliyun.com # 信任,免去SSL验证
disable-pip-version-check = true # 不检查pip版本
default-timeout = 60 # 下载超时时间
[list]
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
➜ ~ pip freeze
backports.entry-points-selectable==1.1.0
certifi==2021.5.30
chardet==4.0.0
cx-Oracle==8.2.1
distlib==0.3.2
filelock==3.0.12
idna==2.10
numpy==1.20.3
pandas==1.2.4
pipenv==2021.5.29
platformdirs==2.0.2
python-dateutil==2.8.1
pytz==2021.1
requests==2.25.1
six==1.16.0
urllib3==1.26.5
virtualenv==20.6.0
virtualenv-clone==0.5.5
生成requirements.txt
pip freeze > requirements.txt
➜ ~ pip freeze > requirements.txt
➜ ~ cat requirements.txt
backports.entry-points-selectable==1.1.0
certifi==2021.5.30
chardet==4.0.0
cx-Oracle==8.2.1
distlib==0.3.2
filelock==3.0.12
idna==2.10
numpy==1.20.3
pandas==1.2.4
pipenv==2021.5.29
platformdirs==2.0.2
python-dateutil==2.8.1
pytz==2021.1
requests==2.25.1
six==1.16.0
urllib3==1.26.5
virtualenv==20.6.0
virtualenv-clone==0.5.5
从requirements.txt安装库
pip install -r requirements.txt
➜ ~ pip install -r requirements.txt
、Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
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)
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 install下载速度慢、易报错
增加超时时间
pip install XXX —default-timeout=1000
更换镜像源
pip install XXX -i http://pypi.douban.com/simple/--trusted-host pypi.douban.com