目录

0.前言

1.临时使用国内源

2.永久修改

2.1方法一:修改配置文件。

2.1.1 Mac/Linux

2.1.1 Windows

2.2方法二:使用命令行(所有系统都适用)

3.写在最后



0.前言

最近需要反复给计算机安装python和anaconda环境,在默认情况下 pip 使用的是国外的软件包源,在下载的时候的速度很慢,花费的时间比较长。故而需要给anaconda和pip安装国内镜像源。这里就简单的记录下我了解的方法,方便我,也方便大家使用。

常见的国内源如下

清华:https://pypi.tuna.tsinghua.edu.cn/simple
中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
阿里云:http://mirrors.aliyun.com/pypi/simple/
豆瓣:http://pypi.douban.com/simple/
百度:https://mirror.baidu.com/pypi/simple

1.临时使用国内源

平时我们在安装的时候都是使用的pip install xxx来安装python包,这里如果是零时使用国内原只需要在后面补上-i xxx,这里xxx为国内源的网址。以使用清华源下载opencv-python为例,如下:

pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple

2.永久修改

2.1方法一:修改配置文件。

2.1.1 Mac/Linux

在Mac/Linux系统下:配置文件位置在 ~/.pip/pip.conf

如果是新安装的就没有这个文件,需要自己创建.pip目录:

mkdir ~/.pip

完成后,打开pip.conf修改为(以清华源为例):

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

最后使用查看是否修改了:

pip config list   

如果出现下面文字就说明修改成功了:

global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
install.trusted-host='https://pypi.tuna.tsinghua.edu.cn'

2.1.1 Windows

        win环境pip的配置文件在C:\Users\xxx\AppData\Roaming\pip\pip.ini里面,可以打开此文件(没有就自己创建一个)直接修改,同样以清华源为例:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn

同样可用pip config list 查看。出现下面的文字就表示成功了

global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
install.trusted-host='https://pypi.tuna.tsinghua.edu.cn'

2.2方法二:使用命令行(所有系统都适用)

输入下面的命令可直接永久设置为清华源。

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

同样可以使用pip config list查看是否设置成功:出现下面的文字就表示成功了

global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
install.trusted-host='https://pypi.tuna.tsinghua.edu.cn'

3.写在最后

上述的例子都是以清华源为例,除此之外,还可以更换为其他所有的国内镜像(替换清华源网址即可)

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐