安装 pip
Python包管理工具(查询、下载、安装、卸载,已安装包导出等),pip官网 「链接」
通常情况下,我们安装Python时就会默认安装pip,如果没有安装可以考虑使用ensurepip安装:
python -m ensurepip --upgrade
如果在windows下升级pip出错(因权限导致终止)导致pip损坏无法使用的情况下,也可以使用ensurepip重新安装修复。
python -m ensurepip
python -m pip install --upgrade pip
使用pip
- 查看版本
pip -V
或者
pip --version
- 查看帮助
pip -h
或者
pip --help
pip 常用命令:
install 安装包.
download 下载包.
uninstall 卸载包.
freeze 用requirements输出已经安装的包(导出环境).
list 已安装包列表.
show 显示安装包的信息.
check 验证已经安装包是否有兼容性依赖项.
config 管理本地和全局配置
search 在PyPI搜索包
cache 检查和管理pip's的wheel缓存
index 检查包索引中的有效信息
wheel 根据需求打包命令(造轮子)(二进制格式)
hash 计算包档案的哈希值
completion pip命令自动补全命令
debug 显示有用的调试信息
help 显示命令的帮助
- 安装包
pip install [options] [package-index-options] ...
pip install [options] -r [package-index-options] ...
pip install [options] [-e] ...
pip install [options] [-e] ...
pip install [options] ...
安装某个包
pip install sampleproject
pip install SomePackage # 最新版本
pip install SomePackage==1.0.4 # 安装指定版本
pip install "SomeProject>=1,<2" # 安装时限定版本范围
pip install 'SomePackage>=1.0.4' # 最小版本安装 大于等于1.0.4
pip install "SomeProject~=1.4.2" # 安装兼容版本 install any version “==1.4.*” version that’s also “>=1.4.2”
pip install --user SomeProject #为当前用户安装
从GitHub安装
pip install git+https://github.com/pypa/sampleproject.git@main
pip install -e git+https://git.repo/some_pkg.git#egg=SomeProject # from git
pip install -e hg+https://hg.repo/some_pkg#egg=SomeProject # from mercurial
pip install -e svn+svn://svn.repo/some_pkg/trunk/#egg=SomeProject # from svn
pip install -e git+https://git.repo/some_pkg.git@feature#egg=SomeProject # from a branch
从本地档案安装
pip install sampleproject-1.0.tar.gz
从本地发行版文件安装
pip install sampleproject-1.0-py3-none-any.whl
# 产生wheel文件
pip install wheel
pip wheel --wheel-dir=/local/wheels -r requirements.txt
pip install --no-index --find-links=/local/wheels -r requirements.txt
通过 requirements 文件安装多个包
pip install -r requirements.txt
pip freeze > requirements.txt # 产生 requirements 文件
通过 Constraints Files 安装
pip install -c constraints.txt
安装包时指定源
pip install --index-url http://my.package.repo/simple/ SomeProject
pip install --extra-index-url http://my.package.repo/simple SomeProject
从本地源码树安装
pip install -e
pip install
从本地目录安装(本地档案库)
pip install --no-index --find-links=file:///local/dir/ SomeProject
pip install --no-index --find-links=/local/dir/ SomeProject
pip install --no-index --find-links=relative/dir/ SomeProject
安装预发行(开发版)
pip install --pre SomeProject
- 卸载包
pip uninstall SomePackage
- 已安装包列表
pip list --outdated
- 显示安装包信息
pip show [options] ...
pip show sphinx
- 查找包
pip search "query"
- 检查安装包的依赖是否完整
pip check flask
- 导出系统已安装的安装包列表到 requirements 文件
pip freeze > requirements.txt
- 查看版本
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
- 本地和局部配置
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
- 下载包
pip download 包名 -d "保存包的本地路径"
- 检查缓存
pip cache
- 查看包索引信息
pip index
- wheel
pip install wheel文件
如:
pip install pyproj-3.1.0-cp38-cp38-win_amd64.whl
- hash
用于计算本地包归档的 sha 值
pip hash [options] ...
如:
pip hash requests-2.19.1-py2.py3-none-any.whl
- debug
显示调试信息
pip debug --verbose
- 命令行自动补全
pip completion --bash >> ~/.bashrc
pip 经常反应慢,以下是几个常用的国内 pip 源
pip install xlrd -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
- http://mirrors.aliyun.com/pypi/simple/ 阿里云
- https://pypi.mirrors.ustc.edu.cn/simple/ 中国科技大学
- http://pypi.douban.com/simple/ 豆瓣
- https://pypi.tuna.tsinghua.edu.cn/simple/ 清华大学
- http://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学
持续学习、适应变化、记录点滴、复盘反思、成长进步