准备工作

1、创建用户和配置环境参数

(1)、创建用户组和创建所需目录

[root@python ~]# groupadd python_env
[root@python ~]# useradd -d /home/python_env -g python_env -m python_env
[root@python ~]# chmod 755 /home/python_env
[root@python ~]# mkdir -p /home/python_env/operation_workspaces
[root@python ~]# mkdir -p /home/python_env/software
[root@python ~]# mkdir -p /home/python_env/yunwei
[root@python ~]# mkdir -p /home/python_env/virtual_environment
[root@python ~]# mkdir -p /data/python_env/yunwei_platforms
[root@python ~]# chown -R python_env:python_env /data/python_env
[root@python ~]# chown -R python_env:python_env /home/python_env

(2)、通过yum安装必要依赖

[root@python ~]# yum install -y gcc* zlib* openssl-devel

切换到普通用户,编译安装python

1、下载

https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz

2、部署

(1)、解压源码包并编译安装

[python_env@python ~]$ tar -zxvf $HOME/software/Python-3.6.5.tgz
[python_env@python ~]$ cd $HOME/software/Python-3.6.5
[python_env@python software/Python-3.6.5]$ ./configure --prefix=$HOME/python3.6.5 --enable-shared --enable-loadable-sqlite-extensions
[python_env@python ~]$ make && make install

(2)、安装完成后解决找不到libpython3.6m.so.1.0的问题

[python_env@python ~]$ $HOME/python3.6.5/bin/python3
/home/python_env/python3.6.5/bin/python3: error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory
[python_env@python ~]$ echo "export LD_LIBRARY_PATH=\"$HOME/python3.6.5/lib:\$LD_LIBRARY_PATH\"" >> $HOME/.bashrc
[python_env@python ~]$ echo "export PATH=\"$HOME/python3.6.5/bin:\$PATH\"" >> $HOME/.bashrc
[python_env@python ~]$ source $HOME/.bashrc
[python_env@python ~]$ $HOME/python3.6.5/bin/python3
Python 3.6.5 (default, Jan  6 2022, 15:53:24) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.

(3)、通过创建软链接替换以前的python环境

[python_env@python ~]$ pip -V
bash: pip: command not found...
[python_env@python ~]$ python -V
Python 2.7.5
[python_env@python ~]$ ln -s $HOME/python3.6.5/bin/python3 $HOME/python3.6.5/bin/python
[python_env@python ~]$ ln -s $HOME/python3.6.5/bin/pip3 $HOME/python3.6.5/bin/pip
[python_env@python ~]$ pip -V
pip 9.0.3 from /home/python_env/python3.6.5/lib/python3.6/site-packages (python 3.6)
[python_env@python ~]$ python -V
Python 3.6.5

安装python虚拟环境

# 创建在当前路径下
[python_env@python ~]$ python -m venv <虚拟环境名称>

# 创建在其他路径下,请注意权限问题,仅可在当前用户下的任意路径创建
[python_env@python ~]$ python -m venv <绝对路径>/<虚拟环境名称>

iM唐小龙
1 声望0 粉丝