- pip 是一个 Python 包安装与管理工具。
- 在Python 是 2.7.9 及以上版本,或 3.4 及以上版本,则已经内置了 pip ,可直接使用。
本示例使用的python版本为:Python 3.9.6
- 查看pip的版本
[root@master ~]# pip -V pip 21.2.4 from /usr/local/python39/lib/python3.9/site-packages/pip (python 3.9)
- 列出已安装的包
[root@master ~]# pip list Package Version ------------------ --------- backcall 0.2.0 certifi 2021.5.30 charset-normalizer 2.0.4 decorator 5.1.0 idna 3.2 ipython 7.28.0 jedi 0.18.0 matplotlib-inline 0.1.3 parso 0.8.2 pexpect 4.8.0 pickleshare 0.7.5 pip 21.2.4 prompt-toolkit 3.0.20 ptyprocess 0.7.0 pyflakes 2.3.1 Pygments 2.10.0 requests 2.26.0 setuptools 56.0.0 traitlets 5.1.0 urllib3 1.26.6 wcwidth 0.2.5
- 格式化输出已安装包的列表
[root@master ~]# pip freeze > test.txt [root@master ~]# cat test.txt backcall==0.2.0 certifi==2021.5.30 charset-normalizer==2.0.4 decorator==5.1.0 idna==3.2 ipython==7.28.0 jedi==0.18.0 matplotlib-inline==0.1.3 parso==0.8.2 pexpect==4.8.0 pickleshare==0.7.5 prompt-toolkit==3.0.20 ptyprocess==0.7.0 pyflakes==2.3.1 Pygments==2.10.0 requests==2.26.0 traitlets==5.1.0 urllib3==1.26.6 wcwidth==0.2.5
- 安装某个包
[root@master ~]# pip install Django //也可以通过使用== 、>=、 <=、 >、 <、来指定版本,不写则安装最新版
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting Django
Downloading https://mirrors.aliyun.com/pypi/packages/22/e6/7c6f9e682578550f748cc33dff04bf812fe0fb7d819fa094a4bb6d2dc2a1/Django-3.2.8-py3-none-any.whl (7.9 MB)
|████████████████████████████████| 7.9 MB 7.0 MB/s
Collecting pytz
Downloading https://mirrors.aliyun.com/pypi/packages/d3/e3/d9f046b5d1c94a3aeab15f1f867aa414f8ee9d196fae6865f1d6a0ee1a0b/pytz-2021.3-py2.py3-none-any.whl (503 kB)
|████████████████████████████████| 503 kB 19.3 MB/s
Collecting sqlparse>=0.2.2
Downloading https://mirrors.aliyun.com/pypi/packages/05/40/d836d55fb3f467243ee839ab7b814822fda522cd395fa41e282684e71ee5/sqlparse-0.4.2-py3-none-any.whl (42 kB)
|████████████████████████████████| 42 kB 1.4 MB/s
Collecting asgiref<4,>=3.3.2
Downloading https://mirrors.aliyun.com/pypi/packages/fe/66/577f32b54c50dcd8dec38447258e82ed327ecb86820d67ae7b3dea784f13/asgiref-3.4.1-py3-none-any.whl (25 kB)
Installing collected packages: sqlparse, pytz, asgiref, Django
Successfully installed Django-3.2.8 asgiref-3.4.1 pytz-2021.3 sqlparse-0.4.2
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[root@master ~]# pip install -r test.txt //可以把要安装的包,写在一个文件中,然后使用-r指定它
[root@master ~]# cat test.txt
backcall==0.2.0
certifi==2021.5.30
charset-normalizer==2.0.4
decorator==5.1.0
idna==3.2
ipython==7.28.0
- 安装本地的包
[root@master ~]# pip install 本地包的路径 //这种方法也是安装本地的包 pip install --no-index -f=<目录>/ <包名> pip install --use-wheel --no-index --find-links=wheelhouse/ <包名>
- 卸载某个包
[root@master ~]# pip uninstall Django -y Found existing installation: Django 3.2.8 Uninstalling Django-3.2.8: Successfully uninstalled Django-3.2.8 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
- 升级某个包
[root@master ~]# pip install pip --upgrade //或者使用pip install -U <包名>
- 显示包所在的目录
[root@master ~]# pip show -f requests Name: requests Version: 2.26.0 Summary: Python HTTP for Humans. Home-page: https://requests.readthedocs.io Author: Kenneth Reitz Author-email: me@kennethreitz.org License: Apache 2.0 Location: /usr/local/python39/lib/python3.9/site-packages Requires: certifi, charset-normalizer, urllib3, idna Required-by: Files: requests-2.26.0.dist-info/INSTALLER requests-2.26.0.dist-info/LICENSE requests-2.26.0.dist-info/metaDATA ......
- 查询可升级的包
[root@master ~]# pip list -o Package Version Latest Type ------------------ --------- --------- ----- certifi 2021.5.30 2021.10.8 wheel charset-normalizer 2.0.4 2.0.6 wheel pyflakes 2.3.1 2.4.0 wheel setuptools 56.0.0 58.2.0 wheel urllib3 1.26.6 1.26.7 wheel
- 只下载包而不安装
pip install <包名> -d <目录> 或 pip install -d <目录> -r 文件
- 打包
pip wheel <包名>
- 更换pip的源
Linux: [root@master ~]# cat ~/.pip/pip.conf [global] timeout = 600 index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com //指定安装时使用的源 pip install <包名> -i https://mirrors.aliyun.com/pypi/simple 在windows上,配置文件为:%HOME%pippip.ini



