解决国内网络Python2.X 3.X PIP安装模块连接超时问题

pip国内的一些镜像

  阿里云 http://mirrors.aliyun.com/pypi/simple/
  中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  豆瓣(douban) http://pypi.douban.com/simple/
  清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
  中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

修改源方法:

临时使用:
可以在使用pip的时候在后面加上-i参数,指定pip源
pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple

永久修改:
linux:
修改 ~/.pip/pip.conf (没有就创建一个), 内容如下:

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

windows:
直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,新建文件pip.ini,内容如下

[global]  
timeout = 6000  
index-url = http://pypi.douban.com/simple  
trusted-host = pypi.douban.com  

懒得自己去换的可以直接运行以下代码

特性:
1.修改下载源
2.屏蔽pip升级提示
3.延长超时等待时间

 1 import os
 2 
 3 ini="""[global]
 4 index-url = https://pypi.doubanio.com/simple/
 5 [install]
 6 trusted-host=pypi.doubanio.com
 7 disable-pip-version-check = true 
 8 timeout = 600
 9 """
10 
11 pippath=os.environ["USERPROFILE"]+"\\pip\\"
12 
13 if not os.path.exists(pippath):
14     os.mkdir(pippath)
15 
16 with open(pippath+"pip.ini","w+") as f:
17     f.write(ini)

这下可以愉快地使用PIP啦~


 

猜你喜欢

转载自www.cnblogs.com/paul-liang/p/9224533.html