简介

pip是python的一个包管理工具,安装完python后就会自带有pip管理工具。
主要记录下pip3的使用和配置方法。

使用

  • 安装Python包

1
2
3
$ pip3 install <pacakge>   # 安装最新版
$ pip3 install -v <pacakge>==<verison> # 安装指定版本
$ python -m pip3 install <pacakge> # 安装最新版
  • 卸载Python包

1
$ pip3 uninstall <pacakge>
  • 列举已安装的包

1
2
$ pip3 list
$ pip3 freeze # 不包括pip等原来环境自带的包
  • 查看包详细信息

1
$ pip3 show <pacakge>
  • 查看更新

1
$ pip3 list --outdated
  • 更新包

1
$ pip3 install --upgrade <package>
  • 搜索仓库的包

1
$ pip3 search <package>
  • 升级pip

1
$ pip3 install --upgrade pip3

配置

  • 修改仓库镜像源地址
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    """
    自动创建pip.ini文件脚本

    修改配置文件实现
    # Windows: C:/Users/<用户名>/pip/pip.ini
    # Linux: ~/.pip/pip.conf

    国内的其他镜像源
    清华大学: https://pypi.tuna.tsinghua.edu.cn/simple/
    阿里云: http://mirrors.aliyun.com/pypi/simple/
    中国科技大学: https://pypi.mirrors.ustc.edu.cn/simple/
    豆瓣: http://pypi.douban.com/simple/
    中国科学技术大学: http://pypi.mirrors.ustc.edu.cn/simple/
    """

    import os

    # pip.ini文件内容以清华为例
    content = """[global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
    [install]
    trusted-host=pypi.tuna.tsinghua.edu.cn
    """

    # 通过 USERPROFILE 环境变量获取用户配置目录
    pipPath = os.environ["USERPROFILE"] + "\\pip\\"
    if not os.path.exists(pipPath):
    os.mkdir(pipPath)

    with open(pipPath + "pip.ini", "w+") as f:
    f.write(content)
    设置成功后可以看到: