pip 常用命令与国内源配置

814 阅读1分钟

pip默认源存在速度慢的问题,本文介绍pip命令添加国内源的方法。

pip常用命令

安装包

pip install Package
pip install -r requirements.txt

更新包

pip install -U Package

卸载包

pip uninstall Package

列出已安装软件

pip list
pip freeze
pip freeze -r requirements.txt

某个包详细信息

pip show -f Package

国内源配置

常用的国内镜像

新版ubuntu要求使用https源,要注意。

就我的经验来说阿里云是目前用下来最稳定可靠的。

临时使用

在使用pip的时候,加上参数 -i 和镜像地址

pip install -i https://mirrors.aliyun.com/pypi/simple/ tensorboard

从清华源安装 tensorboard

命令配置

在终端输入命令:

pip install pip -U    # 升级pip到最新版本
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

此方法配置后安装包时可能会提示下载源不可信,需要手动加上--trusted-host mirrors.aliyun.com

The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.aliyun.com'.

这样就不那么香了,一劳永逸的解决方案是修改pip配置文件

修改配置

Linux

配置文件位置:

  • ~/.pip/pip.conf
  • ~/.config/pip/pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
Windows

配置文件位置:

  • %HOMEPATH%\pip\pip.ini,即 C:\Users\pip\pip.ini
  • %APPDATA%\pip\pip.ini,即C:\Users\AppData\Roaming\pip\pip.ini

可以通过在cmd中输入 echo %APPDATA%echo %HOMEPATH% 查看自己的相关路径

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com

调整源地址

  • http不可信,将源地址修改为 https 也可以避免信任危机
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/