linux更换pip源

以你之姓@ 2023-08-17 16:18 202阅读 0赞

通过几次 pip 的使用,对于默认的 pip 源的速度实在无法忍受,于是便搜集了一些国内的pip源,如下:

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

使用方法很简单,直接 -i 加 url 即可!如下:

  1. # pip install web.py -i http://pypi.douban.com/simple

如果有如下报错:
在这里插入图片描述
请使用命令:

  1. # pip install web.py -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

如果想配置成默认的源,方法如下:

需要创建或修改配置文件(一般都是创建),

  1. linux的文件在~/.pip/pip.conf
  2. windows在%HOMEPATH%\pip\pip.ini),

修改内容为:

  1. [global]
  2. index-url = http://pypi.douban.com/simple
  3. [install]
  4. trusted-host=pypi.douban.com

这样在使用pip来安装时,会默认调用该镜像。

临时使用其他源安装软件包的python脚本如下:

  1. #!/usr/bin/python
  2. import os
  3. package = raw_input("Please input the package which you want to install!\n")
  4. command = "pip install %s -i http://pypi.mirrors.ustc.edu.cn/simple --trusted-host pypi.mirrors.ustc.edu.cn" % package
  5. os.system(command)

也可以使用读入文件进行安装。
ok,仅以记录一下,以便于后期查阅!

  1. #!/usr/bin/python
  2. # coding: utf-8
  3. import platform
  4. import os
  5. os_type = platform.system()
  6. if "Linux" == os_type:
  7. fileDirPath = "%s/.pip" % os.path.expanduser('~')
  8. filePath = "%s/pip.conf" % fileDirPath
  9. if not os.path.isdir(fileDirPath):
  10. os.mkdir(fileDirPath)
  11. fo = open(filePath, "w")
  12. fo.write(
  13. "[global]\nindex-url=https://pypi.tuna.tsinghua.edu.cn/simple/\n[install]\ntrusted-host=pypi.tuna.tsinghua.edu.cn\n")
  14. fo.close()
  15. print "Configuration is complete"
  16. elif "Windows" == os_type:
  17. fileDirPath = "%s\\pip" % os.path.expanduser('~')
  18. filePath = "%s\\pip.ini" % fileDirPath
  19. if not os.path.isdir(fileDirPath):
  20. os.mkdir(fileDirPath)
  21. fo = open(filePath, "w")
  22. fo.write(
  23. "[global]\nindex-url=https://pypi.tuna.tsinghua.edu.cn/simple/\n[install]\ntrusted-host=pypi.tuna.tsinghua.edu.cn\n")
  24. fo.close()
  25. print "Configuration is complete"
  26. else:
  27. exit("Your platform is unknow!")

发表评论

表情:
评论列表 (有 0 条评论,202人围观)

还没有评论,来说两句吧...

相关阅读

    相关 linux更换pip

    通过几次 pip 的使用,对于默认的 pip 源的速度实在无法忍受,于是便搜集了一些国内的pip源,如下: 阿里云 http://mirrors.aliyun.com