python第三方库,国内镜像源(清华&阿里)

Python018

python第三方库,国内镜像源(清华&阿里),第1张

命令

例:

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

国内两个镜像源:

清华: https://pypi.tuna.tsinghua.edu.cn/simple

阿里: http://mirrors.aliyun.com/pypi/simple

如使用 清华镜像 安装 lxml 库的命令如下所示:

在c盘下找到当前用户,进入当前用户命名的文件夹后创建一个新文件夹“pip”,在pip中创建一个文本文件pip.txt,将后缀名改为.ini

pip.ini文件内容(设置国内镜像源,这里使用的是清华镜像,文章后面提供有其他常用镜像)

保存即可,如安装 lxml 库的命令如下所示(默认为清华镜像,不需要加 -i 属性):

        清华:https://pypi.tuna.tsinghua.edu.cn/simple

        阿里云:http://mirrors.aliyun.com/pypi/simple/

        中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

        华中理工大学:http://pypi.hustunique.com/

        山东理工大学:http://pypi.sdutlinux.org/ 

        豆瓣:http://pypi.douban.com/simple/

pip 是 python 必不可少的的包管理工具,但是要在国内用得爽,必须要配置镜像源。

有哪些镜像站可用,以及如何配置,网上都有很多分享了。

我常用的是  阿里云镜像站 。

这里有一点比较麻烦的地方,就是是 Linux 和 Windows 环境下的 pip 配置文件的名字和位置都不同,经常混淆。

今天就教大家一招,快速搞定:

执行完上面两条命令就可以啦。

pip config set 命令能自动把配置写入到用户对应的配置文件中:

[global]

index-url = https://mirrors.aliyun.com/pypi/simple/

[install]

trusted-host=mirrors.aliyun.com

命令虽然方便,但是参数格式复杂,记住不太容易,要是想改一下也挺麻烦,

所以我们进一步了解一下细节。

“ 下面我以 Windows 系统为例,实际在 Linux 系统也是类似

查看配置

执行 pip config list 命令可以显示已经有了哪些配置:

pip config list

global.index-url='http://mirrors.aliyun.com/pypi/simple/'

install.trusted-host='mirrors.aliyun.com'

带上一个 -v 选项:

pip config list -v

For variant 'global', will try loading 'C:\ProgramData\pip\pip.ini'

For variant 'user', will try loading 'C:\Users\davy\pip\pip.ini'

For variant 'user', will try loading 'C:\Users\davy\AppData\Roaming\pip\pip.ini'

For variant 'site', will try loading 'c:\users\davy\appdata\local\programs\python\python38\pip.ini'

global.index-url='http://mirrors.aliyun.com/pypi/simple/'

install.trusted-host='mirrors.aliyun.com'

“ 这里有一点不太好的地方是不显示配置是在哪个文件里。

就把它尝试获取的配置文件名完整路径列出来了。前面的 global 和 user 和 site 分别表示配置文件生效的范围:

global - 全局,一般不用

user - 当前用户,推荐

site - 只针对某一个 python 解释器

可以看到 user 有两个地方,其中配置任何一个都是可以的。

有的网络文章推荐的手动创建文件地址是前面那个,但是 pip 默认创建的是后者。

编辑配置

在命令行直接执行 pip config edit 会自动为我们打开配置文件,但是在 Windows 环境下还不行:

pip config edit

ERROR: Could not determine editor to use.

需要手动指定一个编辑器,就用记事本就行了:

pip config edit  --editor notepad

“ Linux 系统中编辑器可以使用 vi,也可以是你习惯的其它编辑器

如果你从来没有设置过,它会报 找不到指定路径,这是因为相应的文件夹没有创建。

设置配置

通过 pip config set 命令可以直接设置配置项,它会自动创建没有的文件夹和文件。但是必须要给定一个配置项:

pip config set

ERROR: Got unexpected number of arguments, expected 2. (example: "pip config set [name] [value]")

我们随便写一个配置:

pip config set x.y z

Writing to C:\Users\davy\AppData\Roaming\pip\pip.ini

然后再执行上面的

pip config edit  --editor notepad

就能自动打开配置文件,把拷贝好的配置文件内容贴进去就可以啦。