python pip详解1

一、简介

pip是python的一个软件包管理工具,同yum,apt作用一致,pip有两种使用方式:pip模块和pip命令,示例如下:

python -m pip install package
pip install package

二、命令行详解

python -m pip --help

C:\Users\love1>python -m pip --help

Usage:
  C:\Users\love1\AppData\Local\Programs\Python\Python311\python.exe -m pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  inspect                     Inspect the python environment.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  cache                       Inspect and manage pip's wheel cache.
  index                       Inspect information available from package indexes.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  debug                       Show information useful for debugging.
  help                        Show help for commands.

1、安装pip

get-pip

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py   # 下载安装脚本
python get-pip.py    # 运行安装脚本

注意: 用哪个版本的 Python 运行安装脚本,pip 就被关联到哪个版本,如果是 Python3 则执行以下命令:

python3 get-pip.py    # 运行安装脚本

ensurepip

python -m ensurepip
python -m ensurepip --upgrade  # 安装最新版本的

详见安装链接

2、配置

pip通过三种方式来配置相关配置项

  • command line options 命令行选项
  • environment variables 环境变量
  • configuration files 配置文件

配置文件的格式是INI格式

environment variables 环境变量
pip可以通过环境变量来替代命令行选项,格式为PIP_<UPPER_LONG_NAME>,用(_)代替(-),示例如下:

PIP_TIMEOUT=60--timeout=60
PIP_FIND_LINKS="http://mirror1.example.com http://mirror2.example.com"--find-links=http://mirror1.example.com --find-links=http://mirror2.example.com
PIP_VERBOSE=3 同 pip install -vvv

configuration files
有三种层级的配置文件可以用

  • global 系统范围的配置文件,所有用户共享
    该配置文件存放于XDG_CONFIG_DIRS环境变量所代表的目录下,常见目录为/etc/xdg/pip/pip.conf或者/etc/pip.conf
  • user 用户范围的配置文件
    XDG_CONFIG_HOME环境变量所代表的配置文件,常见目录为$HOME/.config/pip/pip.conf或者$HOME/.pip/pip.conf
  • site 环境范围内的配置文件
    $VIRTUAL_ENV/pip.conf

PIP_CONFIG_FILE环境变量也可以指定配置文件路径,但是如果该值被设置为os.devnull,则将禁用所有的配置文件

加载顺序 loading order

  • Global
  • User
  • Site
  • PIP_CONFIG_FILE, if given
    pip会按照上述顺序加载配置文件,但是后加载的配置文件会覆盖前面加载的配置文件,如果配置文件中有相同项配置的话。

在配置文件中按照命令来区分
示例如下

[global]
find-links =
    http://download.example.com
timeout = 60

[freeze]
timeout = 10

[install]
find-links =
    http://mirror1.example.com
    http://mirror2.example.com

trusted-host =
    mirror1.example.com
    mirror2.example.com

上述按照命令区分的含义是,当执行pip freeze的时候,timeout 的值是10而不是60;当执行pip install 的时候timeout 的值是60,但是find-links的值是 http://mirror1.example.com http://mirror2.example.com

生效优先级
命令行选项>环境变量>PIP_CONFIG_FILE配置文件>site配置文件>user 配置文件>global配置文件
在配置文件中 [<command>] host = bar > [<global>] host

–host=foo 会覆盖 PIP_HOST=foo
PIP_HOST=foo 会覆盖配置文件中的[global] host = foo

详见配置文件

  • 28
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值