[Python源码]自动修改pip源

针对国内用户使用pip安装Python包速度缓慢的问题,本脚本提供了一种自动化解决方案,可根据不同操作系统(如Windows、Linux和MacOS)自动配置pip源为阿里云镜像,极大提升了下载速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在国内使用python自带的pip源实在太慢,而根据系统环境不断重复设置过程太过烦琐,因此创建这个自动化脚本,实现自动替换为国内pip源的操作。

最新版本请移步https://github.com/echosun1996/Python-pip-source-changer 获取。

#!/usr/bin/env python
# coding=utf-8
import platform
import os


def mkdir(path):
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        print(path + ' Create finish.')
        return True
    else:
        print(path + ' Directory already exists.')
        return False


def mkfile(filePath):
    pipfile = "[global]\ntrusted-host=mirrors.aliyun.com\nindex-url=http://mirrors.aliyun.com/pypi/simple/"
    if os.path.exists(filePath):
        if str(input("File exist!Cover?(Y/N))")).upper() == 'N':
            print("Not Cover.")
            return
    with open(filePath, 'w') as fp:
        fp.write(pipfile)
    print("Write finish.")


def platformSelect():
    systype = platform.system()
    print("System type: " + systype)
    if systype == "Windows":
        path = os.getenv('HOMEPATH') + "\\pip"
        mkdir(path)
        mkfile(path + '\\pip.ini')

    elif systype == "Linux" or systype == "Darwin":
        path =os.path.expandvars('$HOME')+"/.pip"
        mkdir(path)
        mkfile(path + '/pip.conf')
    else:
        print("System type: " + systype + " Not Support!")


if __name__ == "__main__":
    platformSelect()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值