目录:Python中pip常用命令
一、前言
相信对于大多数熟悉Python的人来说,一定都听说并且使用过pip这个工具,但是对它的了解可能还不一定是非常的透彻,今天就来为大家介绍10个使用pip的小技巧,相信对大家以后管理和使用Python当中的标准库会有帮助。
二、安装
当然在Python 3.4版本之后以及Python 2.7.9版本之后,官网的安装包当中就已经自带了pip
,用户直接在安装完Python之后就可以直接使用。是默认安装的。
如果是需要自己另外安装pip包的,在已经配置好Python的环境当中运行下面这个命令行:
py -m ensurepip --upgrade
另外一种方式是从官网上(https://bootstrap.pypa.io/get-pip.py
)直接下载get-pip.py
脚本,然后直接运行python get-pip.py脚本即可。
三、如何使用
安装后,在命令行中输入pip,然后按下回车,就会出现下图所示的使用说明:
(PyTorch) D:\CodeProject>pip
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
--cert <path> Path to PEM-encoded CA certificate bundle. If provided, overrides the default. See 'SSL Certificate
Verification' in pip documentation for more information.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the certificate in PEM
format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for download.
Implied with --no-index.
--no-color Suppress colored output.
--no-python-version-warning
Silence deprecation warnings for upcoming unsupported Pythons.
--use-feature <feature> Enable new functionality, that may be backward incompatible.
--use-deprecated <feature> Enable deprecated functionality, that will be removed in the future.
3.1 升级
要是你觉得自己的pip版本有点低,想要升级一下的话,在命令行中输入以下命令:
pip install --upgrade pip
或者是:
pip install -U pip
3.2 安装某个版本的包
如果打算用pip来安装第三方的包,用的是以下的命令行:
pip install package-name
例如我们想要安装指定版本的第三方的包,例如安装3.4.1版本的matplotlib:
pip install matplotlib==3.4.1
3.3 卸载或者是更新包
要是你打算想要卸载某个包,该要输入的命令行是:
pip uninstall package_name
而如果打算更新某个包,对应的命令行是:
pip install --upgrade package_name
# 或者是
pip install -U package_name
3.4 查看某个包的信息
可以通过以下的这个命令行来查看指定包的信息:
pip show -f requests
(PyTorch) D:\CodeProject>pip show -f requests
Name: requests
Version: 2.28.1
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: d:\anaconda\envs\pytorch\lib\site-packages
Requires: certifi, charset-normalizer, idna, urllib3
Required-by: msn, requests-oauthlib, tensorboard, torchvision
requests\api.py
requests\auth.py
requests\certs.py
requests\compat.py
requests\cookies.py
requests\exceptions.py
requests\help.py
requests\hooks.py
requests\models.py
requests\packages.py
requests\sessions.py
requests\status_codes.py
requests\structures.py
requests\utils.py
3.5 查看需要被升级的包
我们需要查看一下现有的这些包中,哪些是需要是被升级的,可以用下面这行命令行来查看:
pip list -o
(PyTorch) D:\CodeProject>pip list -o
Package Version Latest Type
----------------------- ----------- --------- -----
absl-py 1.3.0 1.4.0 wheel
asttokens 2.1.0 2.2.1 wheel
attrs 22.1.0 22.2.0 wheel
beautifulsoup4 4.11.1 4.11.2 wheel
bleach 5.0.1 6.0.0 wheel
Bottleneck 1.3.5 1.3.6 wheel
cachetools 5.2.0 5.3.0 wheel
category-encoders 2.5.1.post0 2.6.0 wheel
certifi 2022.9.24 2022.12.7 wheel
charset-normalizer 2.0.4 3.0.1 wheel
cloudpickle 2.2.0 2.2.1 wheel
contourpy 1.0.6 1.0.7 wheel
cryptography 38.0.1 39.0.0 wheel
d2l 0.15.1 0.17.6 wheel
debugpy 1.6.3 1.6.6 wheel
google-auth 2.14.1 2.16.0 wheel
google-auth-oauthlib 0.4.6 0.8.0 wheel
grpcio 1.50.0 1.51.1 wheel
h5py 3.7.0 3.8.0 wheel
imageio 2.23.0 2.25.0 wheel
imbalanced-learn 0.10.0 0.10.1 wheel
importlib-metadata 5.0.0 6.0.0 wheel
ipykernel 6.17.1 6.21.1 wheel
ipython 8.6.0 8.9.0 wheel
ipywidgets 8.0.2 8.0.4 wheel
jedi 0.18.1 0.18.2 wheel
jsonschema 4.17.0 4.17.3 wheel
jupyter_client 7.4.4 8.0.2 wheel
jupyter_core 5.0.0 5.2.0 wheel
jupyter-server 1.23.1 2.2.1 wheel
jupyterlab-widgets 3.0.3 3.0.5 wheel
lightgbm 3.3.3 3.3.5 wheel
MarkupSafe 2.1.1 2.1.2 wheel
matplotlib 3.6.2 3.6.3 wheel
nbclassic 0.4.8 0.5.1 wheel
nbclient 0.7.0 0.7.2 wheel
nbconvert 7.2.4 7.2.9 wheel
nbformat 5.7.0 5.7.3 wheel
networkx 2.8.8 3.0 wheel
numexpr 2.8.3 2.8.4 wheel
numpy 1.23.3 1.24.2 wheel
openpyxl 3.0.10 3.1.0 wheel
packaging 21.3 23.0 wheel
pandas 1.4.4 1.5.3 wheel
Pillow 9.2.0 9.4.0 wheel
pip 22.2.2 23.0 wheel
platformdirs 2.5.3 2.6.2 wheel
prometheus-client 0.15.0 0.16.0 wheel
prompt-toolkit 3.0.32 3.0.36 wheel
protobuf 3.20.3 4.21.12 wheel
Pygments 2.13.0 2.14.0 wheel
pyOpenSSL 22.0.0 23.0.0 wheel
pyrsistent 0.19.2 0.19.3 wheel
pytz 2022.1 2022.7.1 wheel
pywinpty 2.0.9 2.0.10 wheel
pyzmq 24.0.1 25.0.0 wheel
requests 2.28.1 2.28.2 wheel
scikit-learn 1.2.0 1.2.1 wheel
scipy 1.9.3 1.10.0 wheel
seaborn 0.12.1 0.12.2 wheel
setuptools 65.5.0 67.1.0 wheel
stack-data 0.6.0 0.6.2 wheel
tensorboard 2.11.0 2.11.2 wheel
tensorboard-data-server 0.6.1 0.7.0 wheel
terminado 0.17.0 0.17.1 wheel
tifffile 2022.10.10 2023.2.3 wheel
torch 1.12.1 1.13.1 wheel
torchaudio 0.12.1 0.13.1 wheel
torchvision 0.13.1 0.14.1 wheel
traitlets 5.5.0 5.9.0 wheel
typing_extensions 4.3.0 4.4.0 wheel
urllib3 1.26.12 1.26.14 wheel
wcwidth 0.2.5 0.2.6 wheel
websocket-client 1.4.2 1.5.1 wheel
wheel 0.37.1 0.38.4 wheel
widgetsnbextension 4.0.3 4.0.5 wheel
xgboost 1.7.2 1.7.3 wheel
3.6 查看兼容问题
在下载安装一些标准库的时候,需要考虑到兼容问题,一些标准库的安装可能需要依赖其他的标准库,会存在版本相冲突等问题,我们先用下面这条命令行来检查一下是否会有冲突的问题存在:
pip check package_name
当然要是我们不指定是哪个标准库的话,会检查现在已经安装的所有包中的是否存在版本冲突等问题:
pip check
output:
yfinance 0.1.70 has requirement requests>=2.26, but you have requests 2.24.0.
selenium 4.1.0 has requirement urllib3[secure]~=1.26, but you have urllib3 1.25.11.
3.7 指定国内源来安装
我们要是感觉到安装的速度有点慢,可以指定国内的源来安装某个包,例如:
pip install -i https://pypi.douban.com/simple/ package_name
国内源有:
清华: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/
3.8 下载包但是不安装
要是我们想要下载某个包到指定的路径下,命令行如下:
pip download package_name -d "某个路径"
例如:
pip download requests -d "."
就是在当前的目录下下载requests模块以及其他所要依赖的模块。
3.9 批量安装软件包
我们一般在看到别人的项目时,都会包含一个requirements.txt文件,里面包含了一些Python项目当中需要用到的第三方库:
要生成这种txt文件,需要这么来做:
pip freeze > requirements.txt
而如果我们需要来批量安装第三方库,在命令行中输入以下这个命令:
pip install -r requirements.txt