Python使用pip方法安装第三方包时,需要从 https://pypi.org/ 资源库中下载,但是会面临下载速度慢,甚至无法下载的尴尬,这时,就需要配置一个国内源。
-
常用的国内镜像站
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
华为云:https://repo.huaweicloud.com/repository/pypi/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
-
临时使用
在使用pip的时候加参数-i
例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider。
-
永久修改,设为默认
在华为云或者阿里云的镜像站有详细说明,我比较喜欢华为的风格。
像centos、docker等也都可以在华为开源镜像站里找到。
1、Linux平台
(1)创建pip.conf文件
首先运行以下命令
cd ~/.pip # 运行此命令切换目录
如果提示目录不存在,自行创建一个(如果目录存在,可跳过此步),如下:
mkdir ~/.pip
cd ~/.pip
在 .pip目录下创建一个 pip.conf 文件,如下:
touch pip.conf
(2)编辑 pip.conf 文件
首先打开文件,命令如下:
sudo vi ~/.pip/pip.conf
接着,写入以下内容:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn # trusted-host 此参数是为了避免麻烦,否则使用的时候可能会提示不受信任
2、windows平台
(1)新建 pip 配置文件夹,直接在user用户目录中创建一个名为 pip 的文件夹( 即%HOMEPATH%\pip)
(2)在 pip 文件夹中创建一个名为 pip 的文本文件(后缀名由" .txt "改为 " .ini ")
文件内容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn # trusted-host 此参数是为了避免麻烦,否则使用的时候可能会提示不受信任