pip简介
Pip is a package-management system written in Python and is used to install and manage software packages. The Python Software Foundation recommends using pip for installing Python applications and its dependencies during deployment. Pip connects to an online repository of public packages, called the Python Package Index. Pip can be configured to connect to other package repositories (local or remote), provided that they comply to Python Enhancement Proposal 503.
Most distributions of Python come with pip preinstalled. Python 2.7.9 and later (on the python2 series), and Python 3.4 and later include pip by default.
参考文档:
安装pip
低版本的python没有默认安装pip,这时就需要手动安装。
方法一:
1 | python -m ensurepip |
方法二:
1 | wget https://bootstrap.pypa.io/get-pip.py |
查看pip路径
查找默认python的pip路径
1 | which pip |
查看特定python的pip路径,已知python路径为/path/to/python
1 | ls -l /path/to/pip |
pip bin一般和python bin在相同路径。
ModuleNotFoundError处理方法
python 报错 ModuleNotFoundError: No module named ‘xxx’ ,该怎么找到module对应的package?
一般情况下,直接执行pip install安装package即可
1 | pip install xxx |
但是,某些情况下xxx这个module,对应的package名称并不是xxx,这时可以通过搜索引擎或者chatgpt查找xxx这个module对应的package。
module和package的对应关系,可以参考《Python基础》
pip常用命令
安装软件包
1 | pip install requests |
安装软件包,指定pypi进行加速
1 | pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple |
安装指定版本的软件包
1 | pip install requests==2.25.1 |
下载软件包但不安装
1 | pip download requests -d "./" |
查看软件包
1 | pip show requests |
查看需要升级的软件包
1 | pip list -o |
升级软件包
1 | pip install --upgrade requests |
卸载软件包
1 | pip uninstall requests |
查看兼容问题
1 | pip check package_name |