centos7安装python 与卸载python
安装python
下载Python安装包
1
|
cd /usr/local/src |
编译时要提前装好gcc编译器和zlib zlib-devel
1、下载文件
1
|
wget https: //www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz |
2、解压
1
|
tar -zxvf Python-3.6.0.tgz |
3、进入解压目录
1
|
cd Python-3.6.0 |
4、编译安装包
1
2
3
|
./configure --prefix=/usr/local/python make && make install |
5、添加环境变量
1
2
|
echo PATH= '/usr/local/python/bin/:$PATH' >> /etc/profile <br>source /etc/profile |
6、检查是否成功,执行下面代码
1
2
3
4
5
6
7
|
python3.6 Python 3.6.0 ( default , Jun 1 2017, 14:01:43) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux Type "help" , "copyright" , "credits" or "license" for more information. >>> print( 'hello word' ) hello word |
Python安装常见问题:
(1)configure: error: no acceptable C compiler found in $PATH
解决:yum install -y gcc
(2):zipimport.ZipImportError: can’t decompress data
解决:安装zlib zlib-devel
1、修改系统默认的Python路径,因为在终端中输入Python命令时默认是指向Python2.6.6
1
|
mv /usr/bin/python /usr/bin/python-2.6.6 |
2、建立新的软连接,指向Python-3.6.0
1
|
ln -s /usr/local/python/bin/python3.6 /usr/bin/python |
3、因为yum是依赖python的,所以这里我们修改了默认的python,就要要修改yum,让其运行指向旧的版本:
vi /usr/bin/yum
将第一行中的“#!/usr/bin/python” 修改为“#!/usr/bin/python-2.6.6”,保存即可
4、打开一个新的终端,通过python命令进入python环境,可以看到已经指向了我们新安装的python3.6.0:
1
2
3
4
5
|
[centos65_1@localhost:~]$ python Python 3.6.0 ( default , Jul 30 2016, 19:40:32) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin Type "help" , "copyright" , "credits" or "license" for more information. >>> |
5、Centos中Python升级为3.X时yum出现except OSError, e: ^ SyntaxError: invalid syntax问题
CentOS 7升级Python到3.6后,需要在/usr/bin/python创建了一个指向Python 3的软连接,然后将/usr/bin/yum的顶部的:
!/usr/bin/python
改成了
!/usr/bin/python-2.7.5
后,运行yum,还是出现了以下错误:
[root@master ~]# yum -y groupinstall “X Window System”
File "/usr/libexec/urlgrabber-ext-down", line 28
except OSError, e:
^
SyntaxError: invalid syntax
解决方案:
(1). vim /usr/libexec/urlgrabber-ext-down
将/usr/bin/python改为/usr/bin/python-2.7.5。
(2). vim /usr/bin/yum-config-manager
解决办法同上: #!/usr/bin/python换成 #!/usr/bin/python-2.7.5
卸载python
编译安装Python3
# 下载
# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
wget http://mirrors.sohu.com/python/3.6.4/Python-3.6.4.tgz
# 解压
tar -xzvf Python-3.6.4.tgz
cd Python-3.6.4
# 编译安装
# ./configure --help查看编译参数
# 默认安装在'/usr/local/bin','/usr/local/lib' etc
# 可以通过参数指定如:--prefix=/usr/local/python3
./configure
make
# 把安装日志保存下来
make install > install.log
卸载Python3
通过安装日志可知Python3被安装在如下目录了
/usr/local/lib/python3.6 /usr/local/bin/2to3-3.6 to /usr/local/bin/pyvenv-3.6 /usr/local/bin/pydoc3.6 /usr/local/bin/idle3.6
# 删除上述文件
rm -rf /usr/local/lib/python3.6
rm -rf /usr/local/bin/2to3-3.6 to
rm -rf /usr/local/bin/pyvenv-3.6
rm -rf /usr/local/bin/pydoc3.6
rm -rf /usr/local/bin/idle3.6