离线安装pyenv的shell脚本
Posted 白-胖-子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了离线安装pyenv的shell脚本相关的知识,希望对你有一定的参考价值。
Python多版本共存神器-pyenv
- Python最大的问题是版本问题,不光是python2和python3有代差,在python3中的小版本间也有不同版本问题
- 在不适用docker或者虚拟机技术的情况下,实现同一台服务器部署多版本python环境成为了关键
- pyenv很好解决了单机多版本python部署的问题
- pyenv在github上的项目
在线安装pyenv
-
pyenv官方提供了多种安装方式,最方便的就是通过git clone从项目中直接克隆到本机即可。
-
使用普通用户python在线安装pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
或者
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
[python@C8-196 ~]$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Cloning into '/home/python/.pyenv'...
remote: Enumerating objects: 20576, done.
remote: Counting objects: 100% (1600/1600), done.
remote: Compressing objects: 100% (581/581), done.
remote: Total 20576 (delta 963), reused 1470 (delta 894), pack-reused 18976
Receiving objects: 100% (20576/20576), 4.17 MiB | 1.91 MiB/s, done.
Resolving deltas: 100% (13840/13840), done.
- 但是由于你懂的原因,正常情况下几乎不可能成功的!
- 上面的脚本其实就是git了以下几个内容
checkout "$GITHUB/pyenv/pyenv.git" "$PYENV_ROOT" ## 这个是最基础的,必须得有的
checkout "$GITHUB/pyenv/pyenv-doctor.git" "$PYENV_ROOT/plugins/pyenv-doctor" ## 这个是用于doctor得,可以没有的
checkout "$GITHUB/pyenv/pyenv-installer.git" "$PYENV_ROOT/plugins/pyenv-installer" ## 这个是安装器,必须的把自己下载下来
checkout "$GITHUB/pyenv/pyenv-update.git" "$PYENV_ROOT/plugins/pyenv-update" ## 这个update也得有啊!
checkout "$GITHUB/pyenv/pyenv-virtualenv.git" "$PYENV_ROOT/plugins/pyenv-virtualenv" ## 这个是虚拟环境,必须要有呀
checkout "$GITHUB/pyenv/pyenv-which-ext.git" "$PYENV_ROOT/plugins/pyenv-which-ext" ## 这个是个啥玩意
离线安装pyenv
- 先将依赖有的没的都给一股脑装上
yum install gcc make build-essential llvm libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl libncurses5-dev xz-utils libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev tk-dev patch gdbm-devel openssl-devel sqlite-devel redline-devel zlib-devel bzip2-devel -y
- 再创建一个普通用户跑python
useradd python&&echo python | passwd --stdin python
- 切换到普通用户python并下载项目源码包
wget https://github.com/pyenv/pyenv/archive/refs/tags/v2.1.0.tar.gz
[root@C8-196 ~]# su - python
[python@C8-196 ~]$
[python@C8-196 ~]$ wget https://github.com/pyenv/pyenv/archive/refs/tags/v2.1.0.tar.gz
--2021-10-27 09:38:19-- https://github.com/pyenv/pyenv/archive/refs/tags/v2.1.0.tar.gz
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/pyenv/pyenv/tar.gz/refs/tags/v2.1.0 [following]
--2021-10-27 09:38:21-- https://codeload.github.com/pyenv/pyenv/tar.gz/refs/tags/v2.1.0
Resolving codeload.github.com (codeload.github.com)... 20.205.243.165
Connecting to codeload.github.com (codeload.github.com)|20.205.243.165|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/x-gzip]
Saving to: ‘v2.1.0.tar.gz’
v2.1.0.tar.gz [ <=> ] 646.91K 857KB/s in 0.8s
2021-10-27 09:38:23 (857 KB/s) - ‘v2.1.0.tar.gz’ saved [662433]
[python@C8-196 ~]$ ll
total 648
-rw-rw-r-- 1 python python 662433 Oct 27 09:38 v2.1.0.tar.gz
- 编写离线部署pyenv的脚本
#!/usr/bin/env bash
set -e
[ -n "$PYENV_DEBUG" ] && set -x
if [ -z "$PYENV_ROOT" ]; then
PYENV_ROOT="$HOME/.pyenv"
fi
colorize()
if [ -t 1 ]; then printf "\\e[%sm%s\\e[m" "$1" "$2"
else echo -n "$2"
fi
# Checks for `.pyenv` file, and suggests to remove it for installing
if [ -d "$PYENV_ROOT" ]; then
echo
colorize 1 "WARNING"
echo ": Can not proceed with installation. Kindly remove '.pyenv' from $HOME first."
echo
>&2
exit 1
fi
conditional_mv()
[ -d "$2" ] || mkdir -p "$2" && mv "$1"/* "$2"
if ! command -v git 1>/dev/null 2>&1; then
echo "pyenv: Git is not installed, can't continue." >&2
exit 1
fi
# PYENV_PACKAGE_ARCHIVE is the path of pyenv compressed archive file.
if [ -z "$PYENV_PACKAGE_ARCHIVE" ]; then
PYENV_PACKAGE_ARCHIVE="$(cd $(dirname "$0") && pwd)/pyenv-package.tar.gz"
fi
if [ ! -e "$PYENV_PACKAGE_ARCHIVE" ]; then
echo
colorize 1 "ERROR"
echo ": file $PYENV_PACKAGE_ARCHIVE not exists."
echo
>&2
exit 1
fi
# Decompress archive.
TMP_DIR=$(mktemp -d)
tar -xf "$PYENV_PACKAGE_ARCHIVE" -C "$TMP_DIR"
conditional_mv "$TMP_DIR/pyenv" "$PYENV_ROOT"
conditional_mv "$TMP_DIR/pyenv-doctor" "$PYENV_ROOT/plugins/pyenv-doctor"
conditional_mv "$TMP_DIR/pyenv-installer" "$PYENV_ROOT/plugins/pyenv-installer"
conditional_mv "$TMP_DIR/pyenv-update" "$PYENV_ROOT/plugins/pyenv-update"
conditional_mv "$TMP_DIR/pyenv-virtualenv" "$PYENV_ROOT/plugins/pyenv-virtualenv"
conditional_mv "$TMP_DIR/pyenv-which-ext" "$PYENV_ROOT/plugins/pyenv-which-ext"
rm -rf $TMP_DIR
if ! command -v pyenv 1>/dev/null; then
echo
colorize 1 "WARNING"
echo ": seems you still have not added 'pyenv' to the load path."
echo
>&2
# Without args, `init` commands print installation help
"$PYENV_ROOT/bin/pyenv" init || true
"$PYENV_ROOT/bin/pyenv" virtualenv-init || true
>&2
fi
-
我们用各种方法把网上找到的包放进pyenv-package.tar.gz
https://github.com/pyenv/pyenv/archive/refs/tags/v2.1.0.tar.gz
pyenv主程序包
https://github.com/pyenv/pyenv-virtualenv/archive/refs/heads/master.zip
pyenv-virtualenv虚拟环境扩展包
https://github.com/pyenv/pyenv-update/archive/refs/heads/master.zip
pyenv-update升级宝宝
https://github.com/pyenv/pyenv-version-ext/archive/refs/heads/master.zip
pyenv-version-ext版本控制宝宝
https://github.com/pyenv/pyenv-installer/archive/refs/heads/master.zip
pyenv-installer安装pyenv的宝宝
https://github.com/pyenv/pyenv-doctor/archive/refs/heads/master.zip
pyenv-doctor配合docker容器使用的宝宝 -
把这些宝宝其实解压到指定的目录中就行啦
-
添加用户环境变量让这个软件能用
vim ~/.bashrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
source ~/.bashrc
- 测试看看能不能用
[python@C8-196 ~]$ pyenv -v
pyenv 2.1.0-24-ga0cc289d
- 安装好以后更新软件先
[python@C8-196 ~]$ pyenv update
- 创建python安装包存放目录
mkdir ~/.pyenv/cache
- 将需要的python不同版本安装包放到这个目录中
PUrl='https://www.python.org/ftp/python/';for i in 2.7.18,3.5.10,3.6.15,3.7.12,3.8.12,3.9.7,3.10.0;do wget $PUrl$i/Python-$i.tar.xz;done
[python@C8-196 ~]$ cd ~/.pyenv/cache
[python@C8-196 cache]$ ll
total 0
[python@C8-196 cache]$ wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz
[python@C8-196 cache]$ wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz
[python@C8-196 cache]$ wget https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tgz
[python@C8-196 cache]$ wget https://www.python.org/ftp/python/3.7.12/Python-3.7.12.tgz
[python@C8-196 cache]$ wget https://www.python.org/ftp/python/3.6.15/Python-3.6.15.tgz
[python@C8-196 cache]$ wget https://www.python.org/ftp/python/3.5.10/Python-3.5.10.tgz
[python@C8-196 cache]$ wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
[python@C8-196 cache]$ ll -h
total 267M
-rw-rw-r-- 1 python python 187 Oct 27 11:27 downpython.sh
-rw-rw-r-- 1 python python 13M Apr 20 2020 Python-2.7.18.tar.xz
-rw-rw-r-- 1 python python 17M Apr 20 2020 Python-2.7.18.tgz
-rw-rw-r-- 1 python python 18M Oct 5 02:31 Python-3.10.0.tar.xz
-rw-rw-r-- 1 python python 24M Oct 5 02:34 Python-3.10.0.tgz
-rw-rw-r-- 1 python python 15M Sep 5 2020 Python-3.5.10.tar.xz
-rw-rw-r-- 1 python python 20M Sep 5 2020 Python-3.5.10.tgz
-rw-rw-r-- 1 python python 17M Sep 4 14:21 Python-3.6.15.tar.xz
-rw-rw-r-- 1 python python 22M Sep 4 14:20 Python-3.6.15.tgz
-rw-rw-r-- 1 python python 17M Sep 4 13:48 Python-3.7.12.tar.xz
-rw-rw-r-- 1 python python 23M Sep 4 13:47 Python-3.7.12.tgz
-rw-rw-r-- 1 python python 18M Aug 31 00:55 Python-3.8.12.tar.xz
-rw-rw-r-- 1 python python 24M Aug 31 00:55 Python-3.8.12.tgz
-rw-rw-r-- 1 python python 19M Aug 31 03:51 Python-3.9.7.tar.xz
-rw-rw-r-- 1 python python 25M Aug 31 03:52 Python-3.9.7.tgz
- 查看可以安装python版本
- 它不支持的版本你下载了也没用啊
[python@C8-196 ~]$ pyenv install -l
- 使用pyenv安装需要的版本
[python@C8-196 cache]$ for i in 2.7.18,3.5.10,3.6.15,3.7.12,3.8.12,3.9.7,3.10.0;do pyenv install -vvv $i;done
- 查看安装好的python版本
[python@C8-196 cache]$ pyenv version
system (set by /home/python/.pyenv/version)
[python@C8-196 cache]$ pyenv versions
3.10.0
3.5.10
3.6.15
3.7.12
3.8.12
3.9.7
- 很显然,2.7.18没撞上
[python@C8-196 cache]$ pyenv install 2.7.18 -vvv
/tmp/python-build.20211027122550.83864 ~/.pyenv/cache
/tmp/python-build.20211027122550.83864/Python-2.7.18 /tmp/python-build.20211027122550.83864 ~/.pyenv/cache
Installing Python-2.7.18...
/home/python/.pyenv/plugins/python-build/bin/python-build: line 1668: patch: command not found
BUILD FAILED (CentOS Linux 8 using python-build 2.1.0-24-ga0cc289d)
Inspect or clean up the working tree at /tmp/python-build.20211027122550.83864
Results logged to /tmp/python-build.20211027122550.83864.log
Last 10 log lines:
/tmp/python-build.20211027122550.83864 ~/.pyenv/cache
/tmp/python-build.20211027122550.83864/Python-2.7.18 /tmp/python-build.20211027122550.83864 ~/.pyenv/cache
- 创建虚拟环境
- 根据不同项目对python版本的不同要求,创建目录,并在目录中定义python环境
- 先创建项目的目录
[python@C8-196 cache]$ mkdir ~/projects/p3510,p3615,p3712,p3812,p397,p3100 -pv
mkdir: created directory '/home/python/projects'
mkdir: created directory '/home/python/projects/p3510'
mkdir: created directory '/home/python/projects/p3615'
mkdir: created directory '/home/python/projects/p3712'
mkdir: created directory '/home/python/projects/p3812'
mkdir: created directory '/home/python/projects/p397'
mkdir: created directory '/home/python/projects/p3100'
- 在目录中定义python
for i in 2.7.18,3.5.10,3.6.15,3.7.12,3.8.12,3.9.7,3.10.0;do
j=`echo "$i" | sed -r 's/\\.//g'`
pyenv virtualenv $i py$j
mkdir ~/projects/p$j -pv && cd ~/projects/p$j && pyenv local py$j && pyenv version
done
[python@C8-196 ~]$ bash pyv.sh
pyenv: version `py2718' not installed
py3510 (set by /home/python/projects/p3510/.python-version)
py3615 (set by /home/python/projects/p3615/.python-version)
py3712 (set by /home/python/projects/p3712/.python-version)
py3812 (set by /home/python/projects/p3812/.python-version)
py397 (set by /home/python/projects/p397/.python-version)
py3100 (set by /home/python/projects/p3100/.python-version)
- 更改本地pip源为阿里
[python@C8-196 ~]$ mkdir .pip
[python@C8-196 ~]$ vim ~/.pip/pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
以上是关于离线安装pyenv的shell脚本的主要内容,如果未能解决你的问题,请参考以下文章
Linux脚本 03shell脚本离线安装配置集结JDK+InfluxDB+Zookeeper+Kafka(安装文件及脚本源码网盘分享)