当前位置: 首页 > 工具软件 > pip > 使用案例 >

linux命令之pip命令

燕青青
2023-12-01

一、命令简介

  pip 是 Python 包管理工具,该工具提供了对Python 包的查找、下载、安装、卸载的功能。目前如果你在 python.org 下载最新版本的安装包,则是已经自带了该工具。Python 2.7.9 + 或 Python 3.4+ 以上版本都自带 pip 工具。

二、pip命令安装

早先python版本未自带pip工具,需要手动安装。

[root@centos7 ~]# python -V
Python 2.7.5
[root@centos7 ~]# pip -V
-bash: pip: 未找到命令

安装步骤如下:

[root@centos7 ~]# curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1863k 100 1863k 0 0 516k 0 0:00:03 0:00:03 --:–:-- 516k
[root@centos7 ~]# ll
总用量 1868
-rw-------. 1 root root 1420 5月 8 00:12 anaconda-ks.cfg
-rw-r–r--. 1 root root 1908226 5月 18 17:31 get-pip.py
[root@centos7 ~]# python get-pip.py
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Collecting pip<21.0
Downloading pip-20.3.4-py2.py3-none-any.whl (1.5 MB)
|████████████████████████████████| 1.5 MB 222 kB/s
Collecting setuptools<45
Downloading setuptools-44.1.1-py2.py3-none-any.whl (583 kB)
|████████████████████████████████| 583 kB 3.1 MB/s
Collecting wheel
Downloading wheel-0.36.2-py2.py3-none-any.whl (35 kB)
Installing collected packages: pip, setuptools, wheel
Successfully installed pip-20.3.4 setuptools-44.1.1 wheel-0.36.2

检查安装的pip命令版本

[root@centos7 ~]# pip -V
pip 20.3.4 from /usr/lib/python2.7/site-packages/pip (python 2.7)

三、使用示例

1、查看pip版本

(website) [sun@sun-site anaconda3]$ pip -V
pip 21.0.1 from /home/sun/anaconda3/envs/website/lib/python3.9/site-packages/pip (python 3.9)

2、获取命令帮助

(website) [sun@sun-site anaconda3]$ pip -V
pip 21.0.1 from /home/sun/anaconda3/envs/website/lib/python3.9/site-packages/pip (python 3.9)
(website) [sun@sun-site anaconda3]$ pip --help

Usage:
pip <command> [options]

Commands:
install Install packages.
download Download packages.

3、安装软件包最新版

(website) [sun@sun-site anaconda3]$ pip install flask
Collecting flask
Downloading Flask-2.0.0-py3-none-any.whl (93 kB)

Installing collected packages: MarkupSafe, Werkzeug, Jinja2, itsdangerous, click, flask
Successfully installed Jinja2-3.0.1 MarkupSafe-2.0.1 Werkzeug-2.0.1 click-8.0.0 flask-2.0.0 itsdangerous-2.0.1

4、安装软件包指定版本

(website) [sun@sun-site anaconda3]$ pip install django==3.2.1

Installing collected packages: django
Attempting uninstall: django
Found existing installation: Django 3.2.3
Uninstalling Django-3.2.3:
Successfully uninstalled Django-3.2.3
Successfully installed django-3.2.1

5、升级软件包

(website) [sun@sun-site anaconda3]$ pip install --upgrade django
Requirement already satisfied: django in

Attempting uninstall: django
Found existing installation: Django 3.2.1
Uninstalling Django-3.2.1:
Successfully uninstalled Django-3.2.1
Successfully installed django-3.2.3

6、卸载软件包

(website) [sun@sun-site anaconda3]$ pip uninstall flask
Found existing installation: Flask 2.0.0
Uninstalling Flask-2.0.0:
Would remove:
/home/sun/anaconda3/envs/website/bin/flask
/home/sun/anaconda3/envs/website/lib/python3.9/site-packages/Flask-2.0.0.dist-info/*
/home/sun/anaconda3/envs/website/lib/python3.9/site-packages/flask/*
Proceed (y/n)? y
Successfully uninstalled Flask-2.0.0

7、升级pip

(website) [sun@sun-site anaconda3]$ pip install -U pip
Requirement already satisfied: pip in ./envs/website/lib/python3.9/site-packages (21.0.1)
Collecting pip
Downloading pip-21.1.1-py3-none-any.whl (1.5 MB)
|████████████████████████████████| 1.5 MB 591 kB/s
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 21.0.1
Uninstalling pip-21.0.1:
Successfully uninstalled pip-21.0.1
Successfully installed pip-21.1.1

8、查看可升级的包

(website) [sun@sun-site anaconda3]$ pip list -o
Package Version Latest Type
---------- ------------------- ------ -----
setuptools 52.0.0.post20210125 56.2.0 wheel

9、显示安装包信息

(website) [sun@sun-site anaconda3]$ pip show django
Name: Django
Version: 3.2.3
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD-3-Clause
Location: /home/sun/anaconda3/envs/website/lib/python3.9/site-packages
Requires: sqlparse, asgiref, pytz
Required-by:
如果需要更详细信息使用pip show -f

10、列出已安装的包

(website) [sun@sun-site anaconda3]$ pip list
Package Version
------------ -------------------
asgiref 3.3.4
certifi 2020.12.5
click 8.0.0
Django 3.2.3
itsdangerous 2.0.1
Jinja2 3.0.1
MarkupSafe 2.0.1
pip 21.1.1
pytz 2021.1
setuptools 52.0.0.post20210125
sqlparse 0.4.1
Werkzeug 2.0.1
wheel 0.36.2

四、使用语法及参数说明

1、使用语法

用法:pip [options]

2、命令说明

命令命令说明
install安装软件包
download下载软件包
uninstall卸载软件包
freeze按着一定格式输出已安装包列表
list列出已安装的软件包
show显示有关已安装程序包的信息
check检查并验证已安装的软件包是否具有兼容的依赖项
config管理本地和全局配置
search在PyPI中搜索包
cache缓存检查和管理pip的wheel缓存
wheel根据您的要求创建.whl文件
hash散列计算包存档的散列
completion用于命令完成的助手命令
debug调试显示对调试有用的信息
help显示命令帮助

3、参数说明

参数参数说明
-h, --help显示帮助
–isolated在隔离模式下独立运行pip,忽略环境变量和用户配置
-v, --verbose提供更多输出
-V, --version显示版本
-q, --quiet提供更多输出
–log <path>详细附加日志的路径
–no-input禁用输入提示
–proxy <proxy>在表单中指定代理[user:passwd@]代理
–retries <retries>每个连接应尝试的最大重试次数 (默认5次).
–timeout <sec>设置套接字超时(默认为15秒)
–exists-action <action>路径已存在时的默认操作:(s)witch,(i)gnore,(w)ipe,(b)ackup,(a)bort。
–trusted-host <hostname>标记此主机或host:port pair 作为受信任的,即使它没有有效的或任何HTTPS。
–cert <path>到备用CA包的路径。
–client-cert <path>SSL客户端证书的路径,一个包含私钥和PEM格式证书的文件。
–cache-dir <dir>将缓存数据存储在 中。
–no-cache-dir禁用缓存
–disable-pip-version-check禁用pip版本检查
–no-color抑制彩色输出
–no-python-version-warning对即将到来的不受支持的python关闭警告
–use-feature <feature>启用可能向后不兼容的新功能
–use-deprecated <feature>启用弃用的功能,这些功能将在将来被删除
 类似资料: