Python学习笔记:pip使用技巧

一、介绍

pip 可以对 python 第三方库进行安装、更新、卸载等操作,十分方便。

全称为:package installer for python ,也就是 python 包管理工具。

python 各种各样功能的工具库被存放在一个统一的“仓库”里,名叫 PyPiPython Package Index),所有的库安装都是从这里调度。

pip是一个命令行程序,一般都在命令行中执行各种操作。例如:在 Windows 下则是通过 cmd 执行。

二、实操

1.安装pip

python 3.4 及以后,已经内置 pip 工具,无需安装。

# 方法一
easy_install pip

# 方法二
# 下载安装 python scripts目录
# 下载网址:https://pypi.org/project/pip/#files
python setup.py install

2.查看pip版本

pip --version
# pip 21.3.1 from d:\programfiles\anaconda\lib\site-packages\pip (python 3.6)

3.升级pip

pip install --upgrade pip

4.获取帮助

pip help

5.安装库

  • 默认:安装最新版本
pip install package_name
  • 指定版本
pip install package_name==1.1.1
# pip install matplotlib==3.4.1

6.批量安装库

pip install -r D:\\requirements.txt
'''
pickleshare==0.7.5
prompt-toolkit==3.0.18
'''

7.使用wheel文件安装库

离线安装, wheel 文件时库的源文件,可以下载后通过本地安装。

# 离线安装
# 1.找到对应的.whl文件
# https://www.lfd.uci.edu/~gohlke/pythonlibs/

# 2.下载对应的版本

# 3.同目录下cmd执行
pip install matplotlib‑3.4.1‑cp39‑cp39‑win_amd64.whl

8.卸载库

pip uninstall package_name

9.升级库

pip install --upgrade package_name

10.查看库信息

pip show -f package_name
'''
  pandas\tests\series\methods\__pycache__\test_isin.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_nlargest.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_pct_change.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_quantile.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_rank.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_reindex_like.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_rename.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_rename_axis.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_replace.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_reset_index.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_round.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_searchsorted.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_shift.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_sort_index.cpython-36.pyc
  pandas\tests\series\methods\__pycache__\test_sort_values.cpython-36.pyc
'''

11.查看已安装的库

pip list
'''
C:\Users\Hider>pip list
Package                            Version
---------------------------------- ---------
alabaster                          0.7.10
anaconda-client                    1.6.9
anaconda-navigator                 1.7.0
anaconda-project                   0.8.2
asn1crypto                         0.24.0
astroid                            1.6.1
astropy                            2.0.3
attrs                              17.4.0
'''

12.保存库列表

pip freeze > requirements.txt
'''
C:\Users\Hider>pip freeze
alabaster==0.7.10
anaconda-client==1.6.9
anaconda-navigator==1.7.0
anaconda-project==0.8.2
asn1crypto==0.24.0
astroid==1.6.1
astropy==2.0.3
attrs==17.4.0
Babel==2.5.3
backports.shutil-get-terminal-size==1.0.0
'''

13.查看需要升级的库

pip list -o
# 比较慢 需要一个个核查
'''
C:\Users\Hider>pip list -o
Package                  Version   Latest    Type
------------------------ --------- --------- -----
alabaster                0.7.10    0.7.12    wheel
asn1crypto               0.24.0    1.4.0     wheel
astroid                  1.6.1     2.9.3     wheel
astropy                  2.0.3     4.1       wheel
attrs                    17.4.0    21.4.0    wheel
Babel                    2.5.3     2.9.1     wheel
beautifulsoup4           4.6.0     4.10.0    wheel
bitarray                 0.8.1     2.3.5     sdist
'''

14.检查兼容问题

`pip check package_name
'''
C:\Users\Hider>pip check pandas
notebook 5.4.0 requires ipykernel, which is not installed.
jupyter 1.0.0 requires ipykernel, which is not installed.
jupyter-console 5.2.0 requires ipykernel, which is not install
ipywidgets 7.1.1 requires ipykernel, which is not installed.
'''

15.下载库到本地

# 保存为whl格式
pip download package_name -d '路径'
'''
C:\Users\Hider>pip download pandas -d "C:\Users\Hider"
Collecting pandas
  Using cached pandas-1.1.5-cp36-cp36m-win_amd64.whl (8.7 MB)
Collecting numpy>=1.15.4
  Using cached numpy-1.19.5-cp36-cp36m-win_amd64.whl (13.2 MB)
Collecting pytz>=2017.2
  Downloading pytz-2021.3-py2.py3-none-any.whl (503 kB)
     |████████████▌                   | 194 kB 7.4 kB/s eta 0:00:42
     |█████████████                   | 204 kB 7.3 kB/s eta 0:00:41
     |██████████████                  | 215 kB 6.5 kB/s eta 0:00:4
     |██████████████                  | 225 kB 6.5 kB/s eta 0:00:4
'''

16.更换pip源

国外服务器的源速度比较慢,可以从国内的镜像源下载,例如:清华源、豆瓣源、阿里云源等。

但镜像源数据有滞后性,比如清华源的 pypi 镜像每 5 分钟同步一次。

  • 临时使用
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package matplotlib
  • 设置默认
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/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 的 15 个使用小技巧

原文地址:https://www.cnblogs.com/hider/p/15790787.html