npm和pip换源(更换镜像源地址)

1,352 阅读2分钟

npm换源

方法1:命令行

获取当前镜像源地址

// 获取当前的镜像源地址
npm config get registry

我的电脑暂时不知道为什么,不加config也可以,不清楚这样正不正规?或者说其他电脑是不是也这样? 记录:Node.js版本14.15.5, NPM版本为6.14.11

// 这样写也可以
npm get registry

换源

// 这样写也可以
npm config set registry http://registry.npm.taobao.org

// 后面的地址也可以换成这几个:
npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
tencent ------ https://mirrors.cloud.tencent.com/npm/
cnpm --------- https://r.cnpmjs.org/
taobao ------- https://registry.npmmirror.com/
npmMirror ---- https://skimdb.npmjs.com/registry/

没测试过哪个更快,反正用的淘宝源就挺快的 -.-

方法2:nrm

英语学习时间:nrm = NPM registry manager = NPM 注册源管理器,我第一眼看到以为是 Node resource manager...(-.-!!),反正差不多,记住就行...

// 用npm安装nrm
npm install -g nrm

// 列出可用源
nrm ls

npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
tencent ------ https://mirrors.cloud.tencent.com/npm/
cnpm --------- https://r.cnpmjs.org/
taobao ------- https://registry.npmmirror.com/
npmMirror ---- https://skimdb.npmjs.com/registry/

// 使用自己喜欢的镜像源
nrm use taobao

// 然后就会提示
Registry has been set to: https://registry.npmmirror.com/

// 记录一个错误,我输入了 nrm list,没任何反应,说明还是只能用nrm ls...

个人喜欢用nrm,不要问,问就是Life is short...

方法3:临时指定镜像源

目前没这样用过,但还是记录一下

// 以安装express为例
npm install express --registry=https://registry.npm.taobao.org

pip换源

方法1:临时指定

// 写法1:
pip install flask -i http://mirrors.aliyun.com/pypi/simple

// 写法2:
pip install -i http://mirrors.aliyun.com/pypi/simple  flask 

包的名字放前面放后面都可以,记得带上 -i 参数,这种也就是临时用一下,下次安装别的包还得带上,太麻烦。

方法2:改全局配置

每次下载一个包都要指定一下镜像源地址太麻烦,还是要一劳永逸才行,可以通过改系统配置文件来实现:

// Linux系统:
// 目录地址:~/.pip/pip.conf --- 没有就自己创建一个!
// 内容如下:

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
// Windows系统:
// 目录地址:~/pip/pip.ini --- 老规矩,没有就自己创建一个!
// 内容如下:

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

//和上面是一样的

注意:Linux文件是conf后缀,Windows是ini后缀!

但其实这样还是有点麻烦,都不如用pqi来的方便

方法3:pqi

// pip安装pqi
pip install pqi

// 显示可用的镜像源
pqi ls

pypi     https://pypi.python.org/simple/
tuna     https://pypi.tuna.tsinghua.edu.cn/simple
douban   http://pypi.douban.com/simple/
aliyun   https://mirrors.aliyun.com/pypi/simple/
ustc     https://mirrors.ustc.edu.cn/pypi/web/simple

// 选择
pqi use aliyun

记录一个小细节:

// pip 显示已经安装的模块用list参数,输入ls系统是不认识的...
pip list

// pqi 显示可用的镜像源列表用ls参数,输入list系统是不认识的...
pqi ls

个人喜欢pqi,不要问,问就是Life is short