部署堡垒机4——CentOS7 编译安装 Python 3.8.12
1、去python3的官方网站下载源代码
https://www.python.org/downloads/
下载安装Python 3.8.12到/opt/python3
cd /opt
wget https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tar.xz
tar -xvJf Python-3.8.12.tar.xz
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devellibpcap-devel xz-devel
yum install libffi-devel -y
mkdir /opt/python3
cd Python-3.8.12
./configure --prefix=/opt/python3
#如果特别安装过指定路径的高版本的openssl可以用以下命令操作
#./configure --prefix=/opt/python3 --with-openssl=/opt/openssl --with-openssl-rpath=auto
make
make install
导入到全局变量,使得Python3命令可全局执行
echo PATH="/opt/python3/bin:$PATH" >> /etc/profile
source /etc/profile
操作完成后,推荐退出ssh,再重新登录。Python变量source有时候不生效
重新登录后,可以在根目录下直接执行python并用tab键补全,可以看到既包括python2.7,也包括python3.8 ;同样的pip,也如此
root@jumpserver-t01:/root#python
python python2.7 python2-config python3.8 python3-config
python2 python2.7-config python3 python3.8-config python-config
root@jumpserver-t01:/root#pip
pip pip2 pip2.7 pip3 pip3.8
root@jumpserver-t01:/root#
# 设置pip源,可选
清华
python3 -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
阿里
root@jumpserver-t01:/root#cat ~/.pip/pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
# 检查pip源
root@jumpserver-t01:/root#pip3 config list
用pip3安装虚拟环境组件virtualenv
pip3 install virtualenv
为 JumpServer 项目单独创建 python3 虚拟环境。
python3 -m venv /opt/py3
source /opt/py3/bin/activate
每次运行 JumpServer 项目都需要先执行 source /opt/py3/bin/activate
载入此环境。载入后的状态显示如下,会有虚拟环境名称(py3)标注
root@jumpserver-t01:/root#source /opt/py3/bin/activate
(py3) root@jumpserver-t01:/root#
退出环境变量的命令是deactivate
(py3) root@jumpserver-t01:/root#deactivate
root@jumpserver-t01:/root#