CentOS7安装python3.7.1
CentOS不要没事瞎卸载python2.x
CentOS不要没事瞎卸载python2.x
CentOS不要没事瞎卸载python2.x
重要的事情说三遍,至于为什么问google,主要就是很多内置工具都是py2写的。这里只说安装及安装过程中出现的问题
1.获取安装包:Index of /ftp/python/
wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz
2.解压安装包
tar -zxvf Python-3.7.1.tgz
3.将解压好的安装包放到指定目录下(/opt或/usr/bin或/usr/local或/usr/local/src),以/opt为例
mv Python-3.7.1 /opt
4.进入Python-3.7.1
cd Python-3.7.1
5.创建预安装目录(若跳过此步骤将报错make: *** No rule to make target `install’. Stop.)
mkdir /opt/python37
6.设置预安装目录
./configure –-prefix=/opt/python37
7.安装gcc和make(如果已安装可跳过这步)
yum install wget gcc make
8.编译&&安装
make && make install
9.如果以上安装过程中出现的问题解决方法(执行后重复第8部):
# 报错:zipimport.ZipImportError:can’t decompress data;zlib not avariable
yum install zlib-devel
# 报错:No module named '_ctypes'
yum install libffi-devel
10.建立软连接:
# 先删除python2.x和python的软连接(看个人喜好,我喜欢python默认为python3.x,如果不修改默认则建立python3为软连接)
rm /usr/bin/python
# 建立python3和usr/bin/python的软连接
ln -s /opt/python37 /usr/bin/python
这样就可以正常使用了
[root@ ~]# python3
Python 3.7.1 (default, Dec 3 2018, 00:29:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
如果建立python为python3.x的软连的话接这样还存在一个问题,就是yum不能使用了,此时报错
[root@ ~]# yum
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
问题原因在于yum包管理是使用python2.x编写的,此时软连接指向python3.x,python版本语法兼容性导致问题出现。
解决方法一: 不修改python默认软连接
解决方法二: 修改yum包管理文件头为python2或python2.x(x对应系统中python2的版本)
# vim打开/usr/bin/yum
vim /usr/bin/yum
#!/usr/bin/python2
import sys
try:
import yum
except ImportError:
print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
%s
Please install a package which provides this module, or
verify that the module is installed correctly.
It's possible that the above module doesn't match the
current version of Python, which is:
%s
If you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/Faq
""" % (sys.exc_value, sys.version)
sys.exit(1)
sys.path.insert(0, '/usr/share/yum-cli')
try:
import yummain
yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
print >> sys.stderr, "\n\nExiting on user cancel."
sys.exit(1)
:wq 保存推出即可
11.使用python过程中出现的问题解决方法
# 解决 import bz2 报错
yum install bzip2-devel
# 解决 import curses 报错
yum install ncurses-devel
# 解决 import sqlite3 报错
yum install sqlite-devel
# 解决 _dbm _gdbm 缺失提醒
yum install gdbm-devel
# 解决 _lzma 缺失提醒
yum install xz-devel
# 解决 _tkinter 缺失提醒
yum install tk-devel
# 解决 readline 缺失提醒及方向键行为非预期的问题
yum install readline-devel