参考itdada的博客:https://blog.csdn.net/ITdada/article/details/87868972
解决:因为安装的是3.7版本,所以需要首先安装libffi-devel包
yum install libffi-devel -y
python3软连接:
ln -s /usr/local/python3/bin/python3 /usr/bin/python
错误内容:
[root@VM_0_8_centos Python-3.7.2]# pip -list
Traceback (most recent call last):
File "/usr/bin/pip", line 7, in
from pip import main
ImportError: cannot import name 'main' from 'pip' (/usr/local/python3/lib/python3.7/site-packages/pip/__init__.py)
[root@VM_0_8_centos Python-3.7.2]# yum
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
解决:因为/usr/bin目录下的python软连接到了python3.7.2,但是pip和yum需要用到旧版本的python2.7的一些依赖库,pip和yum配置文件的解释器默认用的是/usr/bin目录下的python。我们将软连接手动改回python2.7就可以了。
进入软连接存放位置
cd /usr/bin
查看现有python的软连接指向的版本
ls -al *python*
[root@VM_0_8_centos Python-3.7.2]# cd /usr/bin
[root@VM_0_8_centos bin]# ls -al *python*
lrwxrwxrwx 1 root root 30 Oct 15 09:47 python -> /usr/local/python3/bin/python3
lrwxrwxrwx 1 root root 9 Aug 13 11:02 python2 -> python2.7
-rwxr-xr-x 1 root root 7216 Jun 21 04:28 python2.7
lrwxrwxrwx 1 root root 7 Aug 13 11:02 python275 -> python2
-rwxr-xr-x 1 root root 1835 Jun 21 04:27 python2.7-config
lrwxrwxrwx 1 root root 16 Aug 13 11:02 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 Aug 13 11:02 python-config -> python2-config
先将之前建立的python3的软连接删去
[root@VM_0_8_centos bin]# rm python
rm: remove symbolic link ‘python’? y
[root@VM_0_8_centos bin]# ls -al *python*
lrwxrwxrwx 1 root root 9 Aug 13 11:02 python2 -> python2.7
-rwxr-xr-x 1 root root 7216 Jun 21 04:28 python2.7
lrwxrwxrwx 1 root root 7 Aug 13 11:02 python275 -> python2
-rwxr-xr-x 1 root root 1835 Jun 21 04:27 python2.7-config
lrwxrwxrwx 1 root root 16 Aug 13 11:02 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 Aug 13 11:02 python-config -> python2-config
将之前改的python275再改回python
[root@VM_0_8_centos bin]# mv python275 python
[root@VM_0_8_centos bin]# ls -al *python*
lrwxrwxrwx 1 root root 7 Aug 13 11:02 python -> python2
lrwxrwxrwx 1 root root 9 Aug 13 11:02 python2 -> python2.7
-rwxr-xr-x 1 root root 7216 Jun 21 04:28 python2.7
-rwxr-xr-x 1 root root 1835 Jun 21 04:27 python2.7-config
lrwxrwxrwx 1 root root 16 Aug 13 11:02 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 Aug 13 11:02 python-config -> python2-config
再建立python3的软连接
[root@VM_0_8_centos bin]# ln -s /usr/local/python3/bin/python3.7 python3
[root@VM_0_8_centos bin]# ls -al *python*
lrwxrwxrwx 1 root root 7 Aug 13 11:02 python -> python2
lrwxrwxrwx 1 root root 9 Aug 13 11:02 python2 -> python2.7
-rwxr-xr-x 1 root root 7216 Jun 21 04:28 python2.7
-rwxr-xr-x 1 root root 1835 Jun 21 04:27 python2.7-config
lrwxrwxrwx 1 root root 16 Aug 13 11:02 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 32 Oct 15 10:00 python3 -> /usr/local/python3/bin/python3.7
lrwxrwxrwx 1 root root 14 Aug 13 11:02 python-config -> python2-config
再建立pip3.7的软连接
[root@VM_0_8_centos bin]# ln -s /usr/local/python3/bin/pip3.7 pip3.7
[root@VM_0_8_centos bin]# ls -al *pip*
-rwxr-xr-x. 1 root root 2291 Jul 31 2015 lesspipe.sh
-rwxr-xr-x 1 root root 201 Aug 13 11:03 pip
-rwxr-xr-x 1 root root 201 Aug 13 11:03 pip2
-rwxr-xr-x 1 root root 201 Aug 13 11:03 pip2.7
lrwxrwxrwx 1 root root 29 Oct 15 10:02 pip3.7 -> /usr/local/python3/bin/pip3.7
现在就可以用 python3 和 pip3.7 进行愉快的工作了
[root@VM_0_8_centos bin]# pip3.7 list
Package Version
---------- -------
pip 18.1
setuptools 40.6.2
You are using pip version 18.1, however version 19.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@VM_0_8_centos bin]# python3
Python 3.7.2 (default, Oct 14 2019, 18:28:34)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()