VM环境(Redhat6.7)上已存在python2.6, 使用本地源安装Python3.6
参考链接:Linux 安装python3.7.0 https://www.cnblogs.com/yhongji/p/9383857.html
遇到一个报错 ModuleNotFoundError: No module named '_ctypes'
解决:yum install libffi-devel -y 因为没找到libffi-devel合适的版本,最后安装了Python 3.6.8
一、安装依赖包
1.清理yum环境
发现 yum clean all不好使
/etc/yum.conf中有个路径 cachedir=/var/cache/yum/$basearch/$releasever,把它下面的文件清理了
2.配置yum源
# cat /etc/yum.repos.d/media.repo
[MEDIA]
name=MEDIA
baseurl=file:///var/www/html/pypkg
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
3.把packages/ 放到 /var/www/html/pypkg
/var/www/html/pypkg 下执行 createrepo .
yum --disablerepo=* --enablerepo=MEDIA install gcc
yum --disablerepo=* --enablerepo=MEDIA install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
二、安装python
1.下载python安装包
https://www.python.org/ftp/python
2.解压
tar -xvJf Python-3.6.8.tar.xz
3.建立一个空文件夹,用于存放python3程序
mkdir -p /opt/testpy3/python3
4.执行配置文件,编译,编译安装
cd Python-3.6.8
./configure --prefix=/opt/testpy3/python3
make && make install
5.将python添加到环境变量
# export PATH=/opt/testpy3/python3/bin:$PATH
# echo $PATH
/opt/testpy3/python3/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
注意:上面步这种添加环境变量的方式登出后就会失效,使用下面这种修改.bash_profile的方式
编辑.bash_profile,添加环境变量
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/opt/testpy3/python3/bin
6.验证安装是否成功
注意:执行python会进入python2.6
# python
Python 2.6.6 (r266:84292, May 22 2015, 08:34:51)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
执行python3验证安装是否成功,能进去就OK啦
# python3
Python 3.6.8 (default, Apr 9 2019, 18:09:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
附:Linux设置环境变量PATH
PATH: 可执行程序的查找路径
1)查看当前环境变量:
echo $PATH
2)设置:
方法一: export PATH=PATH:/XXX 但是登出后就失效
方法二:修改~/.bashrc或~/.bash_profile或系统级别的/etc/profile
1. 在其中添加例如export PATH=/opt/testpy3/python3/bin:$PATH
2. source .bashrc (source命令通常用于重新执行刚修改的初始化文件、立刻生效,不必注销重登录)