pyenv使用国内镜像

47 篇文章 1 订阅

据说pyenv不能像pip那样直接切换国内源,需要先把安装包下载到本地,然后install本地安装包

未求证是否真是如此,不过还是花了一点时间,写了一个安装脚本:

vi ~/.pyinstall.py

chmod +x ~/.pyinstall.py

cat ~/.pyinstall.py

#!/usr/bin/env python3
"""
Script for pyenv to install python from huawei mirrors.
Require: python3.7+

Usage::
    cp pyinstall.py ~/.pyinstall.py
    alias pyinstall="~/.pyinstall.py"
    pyinstall 3.10.7
    pyinstall 3.11.0rc1
"""
import functools
import os
import re
import subprocess
import sys
import time
from pathlib import Path

HOST = "https://repo.huaweicloud.com/python/"
DOWNLOAD_URL = HOST + "{}/Python-{}.tar.xz"


def build_url(version: str) -> str:
    slim_version = ".".join(re.findall(r"\d+", version)[:3])
    return DOWNLOAD_URL.format(slim_version, version)


def humanize(seconds: float) -> str:
    if seconds < 60:
        return "{} seconds".format(round(seconds, 1))
    m = int(seconds // 60)
    s = int(seconds % 60)
    if m < 60:
        return "{}m{}s".format(m, s)
    h = m // 60
    m = m % 60
    return "{}h{}m{}s".format(h, m, s)


def say_cost(func):
    @functools.wraps(func)
    def deco(*args, **kwargs):
        start = time.time()
        res = func(*args, **kwargs)
        end = time.time()
        cost = end - start
        print("Function `{}` cost: {}".format(func.__name__, humanize(cost)))
        return res

    return deco


@say_cost
def pyinstall(version: str) -> None:
    cache_dir = Path.home() / ".pyenv" / "cache"
    cmd = "cd {} && (".format(cache_dir)
    url = build_url(version)
    filepath = cache_dir / Path(url).name
    if filepath.exists():
        print(filepath, "exists, skip wget")
    else:
        if not cache_dir.exists():
            cache_dir.mkdir(parents=True)
        cmd += "wget {}&&".format(url)
    cmd += "pyenv install {0};cd -)".format(version)
    print("Start runing ...")
    print("-->", cmd)
    subprocess.run(cmd, shell=True)


def main():
    version = sys.argv[1:] and sys.argv[1]
    if version:
        if version.count(".") > 1:
            pyinstall(version)
        else:
            os.system("pyenv install " + version)
    else:
        os.system("pyenv --help")


if __name__ == "__main__":
    main()

echo 'alias pyinstall=~/.pyinstall.py' >> ~/.zshrc   # 我的是Mac系统

source ~/.zshrc   # 用的是zsh,如果是bash要把.zshrc换成.bashrc

pyinstall 3.7.9

已发出 HTTP 请求,正在等待回应... 200
长度:17389636 (17M) [application/octet-stream]
正在保存至: “Python-3.7.9.tar.xz”

Python-3.7.9.tar.xz    100%[=========================>]  16.58M   460KB/s  用时 67s

2021-12-15 16:58:08 (254 KB/s) - 已保存 “Python-3.7.9.tar.xz” [17389636/17389636])

python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Installing Python-3.7.9...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.7.9 to /Users/xxx/.pyenv/versions/3.7.9

还蛮快的,几分钟就装好了

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值