centos7 安装Python3并配置环境变量亲测有效
python官网(linux下载地址、版本可自选):https://www.python.org/ftp/python/
一、添加阿里云第三方扩展源仓库(安装拓展源仓库才能安装yum 安装 openssl11 openssl11-devel,python源码编译必须要openssl11 或以上版本)
备注,我这里是把pyhton安装包放在 /usr/local/目录下
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
如果执行yum makecache这条命令出现很多Errot 503 还有 Error 404说明没执行成功,如下
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
需要更换yum镜像源后再次执行yum makecache命令
Tips:复制命令的时候提前把撰写窗口打开(查看–撰写–撰写窗口),先复制到撰写窗口里面,再复制到命令行不容易出错
二、安装依赖
yum -y install openssl-devel openssl11 openssl11-devel libffi-devel
yum -y install wget zlib zilb-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make xz-devel libdb-devel
****数据存储压缩模块依赖:xz-devel 可以不安装,数据库模块依赖:xz-devel 可以不安装****
三、设置编译flags以便使用最新的openssl库 --Tips 都没有输出数据
export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)
验证变量配置命令1:echo $CFLAGS
验证变量配置命令2:echo $LDFLAGS
四、配置安装python3,这里以Python-3.10.4为例,其它版本也一样
# 如果要和django版本匹配就必须安装python3.6.0版本,在/usr/local目录下执行
# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tgz
tar -xzvf Python-3.10.4.tgz
cd Python-3.10.4
以命令逐条执行,以便观察构建、编译、安装过程中是否有报错,缺少依赖。
#./configure --prefix=/usr/local/Python3.6.8 --enable-shared
./configure --prefix=/usr/local/Python3.10.4 --enable-shared
make
make install
##--enable-share 编译配置中添加共享库,否则使用python3 命令时系统会提示 无法找到 libpython3.9.so.1.0 的错误
安装成功输出如下:
Installing collected packages: setuptools, pip
WARNING: The scripts pip3 and pip3.10 are installed in '/usr/local/python3.10.4/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-22.0.4 setuptools-58.1.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
五、配置python环境变量
查看python3根目录安装路径(以安装成功的上图中的地址为准)
配置环境变量
Python3.10版本
export PATH=/usr/local/python3.10.4/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/python3.10.4/lib:$LD_LIBRARY_PATH
Python3.6版本
export PATH=/usr/local/Python3.6.8/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/Python3.6.8/lib:$LD_LIBRARY_PATH
上面是临时加入,永久加入环境变量需要手动加入
进行编辑: vi /etc/profile
然后复制上面2行后,进行保存
执行立即生效
source /etc/profile
1
验证python3 是否可用
[root@VM-node1 ~]# python3
Python 3.10.4 (main, Oct 9 2023, 17:40:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
验证pip3 命令是否可用
pip3 -V
pip 22.3 from /usr/local/python3.10.0/lib/python3.10/site-packages/pip (python 3.10)
六、更新pip设置为阿里源
pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple
pip3 config set install.trusted-host mirrors.aliyun.com
七、更新pip源到新版本
python3 -m pip install --upgrade pip
# 返回Successfully uninstalled pip更新成功
Collecting pip
Downloading https://mirrors.aliyun.com/pypi/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB)
100% |████████████████████████████████| 1.7MB 4.2MB/s
Installing collected packages: pip
Found existing installation: pip 18.1
Uninstalling pip-18.1:
Successfully uninstalled pip
标签:python,devel,centos7,--,usr,pip,local,环境变量,Python3 From: https://www.cnblogs.com/wangqinghua/p/18458293