python pip 换源
import platform
import os
def change_source(sourch_path, sourch_host):
"""
pip 换源
:param sourch_path:源网址
:param sourch_host:源主机
:return:
"""
os_type = platform.system()
if "Linux" == os_type:
fileDirPath = "%s/.pip" % os.path.expanduser('~')
filePath = "%s/pip.conf" % fileDirPath
if not os.path.isdir(fileDirPath):
os.mkdir(fileDirPath)
fo = open(filePath, "w")
fo.write(
"[global]\nindex-url="
+ sourch_path
+ "\n[install]\ntrusted-host="
+ sourch_host
+ "\n"
)
fo.close()
elif "Windows" == os_type:
fileDirPath = "%s\\pip" % os.path.expanduser('~')
filePath = "%s\\pip.ini" % fileDirPath
if not os.path.isdir(fileDirPath):
os.mkdir(fileDirPath)
fo = open(filePath, "w")
fo.write(
"[global]\nindex-url="
+ sourch_path
+ "\n[install]\ntrusted-host="
+ sourch_host
+ "\n"
)
fo.close()
else:
exit("Your platform is unknow!")
if __name__ == '__main__':
sourch_path = "https://pypi.tuna.tsinghua.edu.cn/simple/"
sourch_host = "pypi.tuna.tsinghua.edu.cn"
change_source(sourch_path, sourch_host)