pip命令以及常用镜像源

1.pip工具

pip是python包管理工具,提供了对python包的下载、安装、卸载等功能。
python安装包自带该工具,无需单独安装。
可以通过如下命令查看pip版本:

pip -V

若未安装,可以通过如下命令安装.

Linux中

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

部分Linux发行版可直接用包管理器安装pip,如Debian和Ubuntu:

sudo apt-get install python-pip
或
sudo apt-get install python3-pip

也可以使用如下命令(可以了解一下apt和apt-get的区别):

sudo apt install python-pip
或
sudo apt install python3-pip

注意:若安装过程中出现问题Unable to locate package python3-pip,此时只需要执行如下更新命令,然后安装即可:

sudo apt-get update

windows中

(1)下载pip压缩安装文件:https://pypi.python.org/pypi/pip#downloads

(2)解压运行setup文件:进入该目录,执行命令python setup.py install

(3)输入命令pip -V查看是否安装成功,并查看是否需要配置环境变量。

linux中也可以下载源文件进行安装,安装命令改为sudo python setup.py install即可。

2.常用命令

  1. 显示版本和路径

    pip --version
    或
    pip -V
    #pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
    
  2. 获取帮助

    pip --help
    
    ###
    Usage:
      pip <command> [options]
    
    Commands:
      install                     Install packages.
      download                    Download packages.
      uninstall                   Uninstall packages.
      freeze                      Output installed packages in requirements format.
      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.
      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.
    
    General Options:
      -h, --help                  Show help.
      --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
      -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
      -V, --version               Show version and exit.
      -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging
                                  levels).
      --log                 Path to a verbose appending log.
      --proxy              Specify a proxy in the form [user:passwd@]proxy.server:port.
      --retries          Maximum number of retries each connection should attempt (default 5 times).
      --timeout              Set the socket timeout (default 15 seconds).
      --exists-action     Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
      --trusted-host    Mark this host or host:port pair as trusted, even though it does not have valid or any HTTPS.
      --cert                Path to alternate CA bundle.
      --client-cert         Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
      --cache-dir            Store the cache data in .
      --no-cache-dir              Disable the cache.
      --disable-pip-version-check
                                  Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index.
      --no-color                  Suppress colored output
      --no-python-version-warning
                                  Silence deprecation warnings for upcoming unsupported Pythons.
    ###
    
  3. 升级pip工具

    pip install -U pip
    
    sudo easy-install --upgrade pip
    

    以上命令可能会因为权限不足导致升级失败,尽量使用如下命令:

    linux中:

    pip install --upgrade pip
    

    windows中:

    python -m pip install --upgrade pip
    
  4. 安装在线python包

    pip install 包名              # 最新版本
    pip install 包名==1.0.4       # 指定版本
    pip install '包名>=1.0.4'     # 最小版本
    
  5. 安装离线python

    pip install 包文件名.whl
    
  6. 升级python包

    pip install --upgrade 包名
    
  7. 卸载python包

    pip uninstall 包名
    
  8. 查看python包信息

    pip show 包名
    
    ###
    pip show pip
    Name: pip
    Version: 20.3.1
    Summary: The PyPA recommended tool for installing Python packages.
    Home-page: https://pip.pypa.io/
    Author: The pip developers
    Author-email: [email protected]
    License: MIT
    Location: d:\workspace\anaconda3\lib\site-packages\pip-20.3.1-py3.7.egg
    Requires:
    Required-by:
    ###
    
  9. 查看所有已安装的python包

    pip list
    

3.安装源与国内镜像

  1. 官方源

    https://pypi.python.org/simple
    
  2. 国内镜像

    阿里云 http://mirrors.aliyun.com/pypi/simple/
    中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
    豆瓣 https://pypi.douban.com/simple
    中国科学院 http://pypi.mirrors.opencas.cn/simple/
    清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
    山东理工大学 http://pypi.sdutlinux.org/
    华中理工大学 http://pypi.hustunique.com/
    
  3. 临时使用镜像源

    pip install 包名 -i 镜像源
    
  4. 命令修改默认源

    pip install pip -U #先将pip升级到最新版
    pip config global.index-url 镜像源 #再进行配置全局安装源
    
  5. 全局配置文件修改默认源

    在windows文件管理器中输入%APPDATA%
    进入到pip文件夹里面,新建一个pip.ini文件,输入如下内容后保存:

    [global]
    timeout = 6000
    index-url = http://mirrors.aliyun.com/pypi/simple/
    trusted-host = mirrors.aliyun.com
    

Notice

可加群讨论【码农交流所】:https://jq.qq.com/?_wv=1027&k=kPpOeOop
在这里插入图片描述

你可能感兴趣的:(python,python模块,安装,python,pip,python包镜像,国内镜像源)