据说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
还蛮快的,几分钟就装好了