pip 是 Python 包管理工具,该工具提供了对Python 包的查找、下载、安装、卸载的功能。熟练使用此工具,也是python的基本功。https://pypi.org目前最新版本为20.2.2,以下就以此版本来演示其使用。
下面演示,是在windows10环境下,通过virtualenv工具建立的python虚拟环境中进行。
- 启动虚拟环境(虚拟环境,建立在D:workpythonestpip下。运行cmd,进入console,进入此目录)
![1efc244e4176ed71da26a7e93a0663d3.png](https://img-blog.csdnimg.cn/img_convert/1efc244e4176ed71da26a7e93a0663d3.png)
运行venvscriptsactivate命令,启动虚拟环境
![5e8a3f1da070a16f77713261833014f7.png](https://img-blog.csdnimg.cn/img_convert/5e8a3f1da070a16f77713261833014f7.png)
虚拟环境启动成功,路径前缀,多出(venv)
pip -V 显示pip版本
![390250b49d3912eca53892e1072a1698.png](https://img-blog.csdnimg.cn/img_convert/390250b49d3912eca53892e1072a1698.png)
pip list 列出已经安装的包
![894fc2fe8fc0346ad7c68e5bf66cb6ef.png](https://img-blog.csdnimg.cn/img_convert/894fc2fe8fc0346ad7c68e5bf66cb6ef.png)
pip show numpy 显示特定安装包的信息
![8afce7e20aa1c0babc16987c4b7412ae.png](https://img-blog.csdnimg.cn/img_convert/8afce7e20aa1c0babc16987c4b7412ae.png)
pip check tensorflow 检查依赖包是否已经安装
![018976297cb930b54a919dd12bccd716.png](https://img-blog.csdnimg.cn/img_convert/018976297cb930b54a919dd12bccd716.png)
pip hash numpy-1.19.1-cp36-cp36m-win_amd64.whl 计算安装包的hash值
![6595a1a08383544200b26fbe677c674c.png](https://img-blog.csdnimg.cn/img_convert/6595a1a08383544200b26fbe677c674c.png)
【在线安装】从网上下载安装包来安装
pip install numpy 安装numpy包
![e73fc331c8eaed4ed98a47a772d6c99d.png](https://img-blog.csdnimg.cn/img_convert/e73fc331c8eaed4ed98a47a772d6c99d.png)
pip install --upgrade tensorflow 或 pip install -U tensorflow
安装最新版本tensorflow(升级)
pip install numpy==1.13.3 安装指定版本包
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow
指定从清华服务器下载tensorflow 安装包。等效于后面的配置文件中index-url设置。
pip install -r requirements.txt 根据包需求文件批量安装,常用于复现环境
【requirements.txt内容格式为】
APScheduler==2.1.2
Django==1.5.4
MySQL-Connector-Python==2.0.1
MySQL-python==1.2.3
PIL==1.1.7
South>=1.0.2
django-grappelli==2.6.3
django-pagination==1.0.7
【本地安装】已经下载了安装包(whl文件)
pip install numpy-1.19.1-cp36-cp36m-win_amd64.whl 安装当前目录中的wheel安装包文件
卸载安装包pip uninstall numpy 卸载(加-y参数,自动完成,不再询问y/n)
![ee26f93a93574b51f5274c71d5642861.png](https://img-blog.csdnimg.cn/img_convert/ee26f93a93574b51f5274c71d5642861.png)
pip uninstall -r requirements.txt -y 自动卸载requirements.txt中所有包(批量卸载)
下载安装包文件pip download numpy
下载numpy包,执行后,在当前目录中下载numpy-1.19.1-cp36-cp36m-win_amd64.whl
![6e7eea453c64fc42f4fc538b7a25143e.png](https://img-blog.csdnimg.cn/img_convert/6e7eea453c64fc42f4fc538b7a25143e.png)
pip freeze >requirements.txt
将当前环境安装的包的版本信息输出到文件,供复现环境用
![e64daa4c29193a3a28e8b97a0d836693.png](https://img-blog.csdnimg.cn/img_convert/e64daa4c29193a3a28e8b97a0d836693.png)
pip cache dir
windows下的缓存包位置 c:甥敳獲administratorappdatalocalpipcachewheels
pip cache list 列出当前缓存的安装包
![b5f0773f18dc37cbc53b302c2ed5114b.png](https://img-blog.csdnimg.cn/img_convert/b5f0773f18dc37cbc53b302c2ed5114b.png)
pip cache info 显示缓存占用磁盘大小信息
pip cache remove gym* 删除缓存中的gym开头的包
![5c837f84e53f49f8587b10c548bd879b.png](https://img-blog.csdnimg.cn/img_convert/5c837f84e53f49f8587b10c548bd879b.png)
pip cache purge 删全部缓存中包
![d216943404e68cda70c1fc3d16f5565f.png](https://img-blog.csdnimg.cn/img_convert/d216943404e68cda70c1fc3d16f5565f.png)
pip wheel -r requirements.txt --wheel-dir=./wheelhouse
在当前目录下的wheelhouse目录,下载requirements.txt中所有包
*关于pip配置文件:
使用pip时,有很多命令行系统参数,可以将这些参数预先设置到配置文件中。windows与linux的配置文件位置不同,但内容相同。
linux系统中,配置文件为: ~/.pip/pip.conf
windows系统中,配置文件为: c:甥敳獲当前用户pippip.ini
![86a3948e3300b6c58eee5f74248fe02b.png](https://img-blog.csdnimg.cn/img_convert/86a3948e3300b6c58eee5f74248fe02b.png)
windows10中pip.ini位置
下面为pip.ini的设置实例
[global]timeout = 6000index-url = https://pypi.tuna.tsinghua.edu.cn/simple[install]trusted-host = pypi.tuna.tsinghua.edu.cn
timeout = 6000 pip从网络下载安装包时的超时时间,以秒为单位。pip默认的安装,是从国外下载包,比较慢,常出现超时错误。可以设置更长一些。
index-url = https://pypi.tuna.tsinghua.edu.cn/simple 设置为从国内清华的服务器下载安装包,更快。
trusted-host = pypi.tuna.tsinghua.edu.cn 信任清华服务器,防止因安全性检查造成中断。
![24abad37beac8e8acd91f74e11d408b8.png](https://img-blog.csdnimg.cn/img_convert/24abad37beac8e8acd91f74e11d408b8.png)
未改配置文件前,从默认国外下载tensorflow包,估计用时28分
![7ef6c08f6bc062e6638efd8151ea1bc5.png](https://img-blog.csdnimg.cn/img_convert/7ef6c08f6bc062e6638efd8151ea1bc5.png)
修改配置后,以后pip默认从https://pypi.tuna.tsinghua.edu.cn/simple下载安装包,估计用时4分钟