欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS 7 安装Python3的配置

程序员文章站 2022-05-29 12:14:39
...

接着:
Python3.7源码包编译安装 https://blog.csdn.net/jiduochou963/article/details/86694775

一、python部分

切换(修改默认链接):

$ cd /usr/bin/
$ mv python python.back
$ mv python-config python-config.back

确认一下:

[[email protected] bin]# ll | grep python
lrwxrwxrwx.   1 root root           9 3月  22 09:52 python2 -> python2.7
-rwxr-xr-x.   1 root root        7216 10月 31 07:46 python2.7
-rwxr-xr-x.   1 root root        1835 10月 31 07:46 python2.7-config
lrwxrwxrwx.   1 root root          16 3月  24 12:37 python2-config -> python2.7-config
lrwxrwxrwx.   1 root root           7 3月  22 09:52 python.back -> python2
lrwxrwxrwx.   1 root root          14 3月  24 12:37 python-config.back -> python2-config
[[email protected] bin]# 

然后设置硬链接:

$ cd /usr/local/python3/bin
$ ln python3.7 /usr/bin/python3.7
$ ln python3.7m-config /usr/bin/python3.7m-config

确认一下:

[[email protected] bin]# cd /usr/bin/
[[email protected] bin]# ll | grep python
lrwxrwxrwx.   1 root root           9 3月  22 09:52 python2 -> python2.7
-rwxr-xr-x.   1 root root        7216 10月 31 07:46 python2.7
-rwxr-xr-x.   1 root root        1835 10月 31 07:46 python2.7-config
lrwxrwxrwx.   1 root root          16 3月  24 12:37 python2-config -> python2.7-config
-rwxr-xr-x.   3 root root    13979392 3月  24 11:32 python3.7
-rwxr-xr-x.   2 root root        3105 3月  24 11:33 python3.7m-config
lrwxrwxrwx.   1 root root           7 3月  22 09:52 python.back -> python2
lrwxrwxrwx.   1 root root          14 3月  24 12:37 python-config.back -> python2-config

设置软链接:

$ ln -s python3.7 python
$ ln -s python3.7 python3
$ ln -s python3.7m-config python-config
$ ln -s python3.7m-config python3-config

确认一下:

[[email protected] bin]# ll | grep python
lrwxrwxrwx.   1 root root           9 3月  24 20:54 python -> python3.7
lrwxrwxrwx.   1 root root           9 3月  22 09:52 python2 -> python2.7
-rwxr-xr-x.   1 root root        7216 10月 31 07:46 python2.7
-rwxr-xr-x.   1 root root        1835 10月 31 07:46 python2.7-config
lrwxrwxrwx.   1 root root          16 3月  24 12:37 python2-config -> python2.7-config
lrwxrwxrwx.   1 root root           9 3月  24 20:54 python3 -> python3.7
-rwxr-xr-x.   3 root root    13979392 3月  24 11:32 python3.7
-rwxr-xr-x.   2 root root        3105 3月  24 11:33 python3.7m-config
lrwxrwxrwx.   1 root root          17 3月  24 21:16 python3-config -> python3.7m-config
lrwxrwxrwx.   1 root root           7 3月  22 09:52 python.back -> python2
lrwxrwxrwx.   1 root root          17 3月  24 20:55 python-config -> python3.7m-config
lrwxrwxrwx.   1 root root          14 3月  24 12:37 python-config.back -> python2-config
[[email protected] bin]# 

二、pip部分

设置硬链接:

[[email protected] bin]# pwd
/usr/local/python3/bin
[[email protected] bin]# ln pip3 /usr/bin/pip3

确认并设置软链接:

[[email protected] bin]# ll | grep pip
-rwxr-xr-x.   1 root root        2291 7月  31 2015 lesspipe.sh
[[email protected] bin]# pwd
/usr/bin
[[email protected] bin]# ll | grep pip
-rwxr-xr-x.   1 root root        2291 7月  31 2015 lesspipe.sh
-rwxr-xr-x.   2 root root         232 3月  24 11:35 pip3
[[email protected] bin]# ln -s pip3 pip
[[email protected] bin]# ll | grep pip 
-rwxr-xr-x.   1 root root        2291 7月  31 2015 lesspipe.sh
lrwxrwxrwx.   1 root root           4 3月  24 21:29 pip -> pip3
-rwxr-xr-x.   2 root root         232 3月  24 11:35 pip3
[[email protected] bin]# 

三、其他注意事项

切换到 python3 后,可能会造成一些命令不能使用,如:yum。
这是因为这些命令不兼容 python3,只需要将其修改为使用原先的python2即可。

把文件头部的#!/usr/bin/python改成#!/usr/bin/python2.7

[[email protected] onefine]# yum
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax
[[email protected] onefine]# cd /usr/bin/
[[email protected] bin]# cat yum 
#!/usr/bin/python
...省略
[[email protected] bin]# vim yum
#!/usr/bin/python2.7
...省略
[[email protected] bin]# cat yum
#!/usr/bin/python2.7
...省略
[[email protected] bin]# 
[[email protected] bin]# yum
已加载插件:fastestmirror
您需要给出命令
Usage: yum [options] COMMAND

List of Commands:

check          检查 RPM 数据库问题
...省略

四、测试

[[email protected] ~]# python --version
Python 3.7.2
[[email protected] ~]# pip --version
pip 19.0.3 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
[[email protected] ~]# cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

[[email protected] ~]# 

参考:

Linux下切换Python版本 https://blog.csdn.net/hangdongzhang/article/details/78405483

防火墙不能启动请参阅:
Centos7 安装python3详细教程,解决升级后不兼容问题 https://blog.51cto.com/9406836/2140783

相关标签: CentOS7 Python3