您现在的位置是:首页 > 正文

Linux 安装 python3 并保留 python2 环境

2024-02-01 00:57:10阅读 2

Linux 安装 python3 并保留 python2 环境

注意

  • 不能卸载Linux系统下的python2.x版本,否则会导致yum和firewall防火墙不能使用

查看当前系统的python版本

which python

下载python3.x

  • 官方地址:https://www.python.org/downloads/
  • 官方下载地址:https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz

安装

  • 安装依赖库
yum -y install zlib* bzip2-devel libffi-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
  • 解压缩
cd /usr/local/src
tar -zxvf Python-3.7.9.tgz
  • 编译安装
cd /usr/local/src/Python-3.7.9
./configure --prefix=/usr/local/opt/python3.7.9 \
--with-openssl=/usr/local/opt/openssl \
--enable-shared
echo $?
make
echo $?
make install
echo $?

配置软链接或者系统环境变量(二选一)

添加python3符号软链接
ln -s /usr/local/opt/python3.7.9/bin/python3 /usr/bin/python3
ln -s /usr/local/opt/python3.7.9/bin/pip3 /usr/bin/pip3
配置系统环境变量
vi /etc/profile

export PYTHON_HOME=/usr/local/opt/python3.7.9
export PATH=$PATH:$PYTHON_HOME/bin
  • 刷新bash配置文件(在不重启系统的环境下)
source /etc/profile

共享python-devel的so库

  • 查看so库是否生成
cd /usr/local/opt/python3.7.9/lib
ll

显示

lrwxrwxrwx  1 root root       20 Jan  2 08:27 libpython3.7m.so -> libpython3.7m.so.1.0
-r-xr-xr-x  1 root root 13714312 Jan  2 08:27 libpython3.7m.so.1.0
-r-xr-xr-x  1 root root     7680 Jan  2 08:27 libpython3.so
  • 将so库添加到系统
vi /etc/ld.so.conf
cp -a /etc/ld.so.conf /etc/ld.so.conf.bak
echo "/usr/local/opt/python3.7.9/lib" >> /etc/ld.so.conf
# 建立动态链接
ldconfig -v

测试

ll /usr/bin | grep python

显示

lrwxrwxrwx    1 root root           7 May 15  2020 python -> python2
lrwxrwxrwx    1 root root           9 May 15  2020 python2 -> python2.7
-rwxr-xr-x    1 root root        7144 Apr  2  2020 python2.7
lrwxrwxrwx    1 root root          38 May 10 11:13 python3 -> /usr/local/opt/python3.9.2/bin/python3
  • 查看python2版本号
python2 -V
  • 查看python3版本号
python3 -V

网站文章