【已解决】Ubuntu18.04用python报错bash: ’/usr/bin/python2.7’: 没有那个文件或目录
- 问题描述
-
- 系统配置
- 遇到的问题
- 解决方法
问题描述
系统配置
系统配置:Ubuntu18.04,Vmware14。
我在系统上同时安装了python2.7与python3.6,两个版本的Python共存。
遇到的问题
Linux的许多发行版本(如Ubuntu)都会自带Python2.7,但是当我们准备开发一个Python3项目的时候,我们该怎么办?
当我使用 python
指令与利用 python
进行操作时出现如下错误:
virtual-machine:~$ python
//报错
bash: ’/usr/bin/python2.7’: 没有那个文件或目录
virtual-machine:~$ python2
//没有报错
Python 2.7.11 (default, Jul 10 2021, 23:36:37)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
virtual-machine:~$ python2.7
//没有报错
Python 2.7.11 (default, Jul 10 2021, 23:36:37)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
virtual-machine:~$ python3
//没有报错
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
virtual-machine:~$ whereis python
//系统中python所在路径
python: /usr/bin/python2.7 /usr/bin/python3.6-config /usr/bin/python
/usr/bin/python3.6m /usr/bin/python2.7-config /usr/bin/python3.6m-config
/usr/bin/python3.6 /usr/bin/python.bak /usr/lib/x86_64-linux-gnu/python2.7
/usr/lib/python3.7 /usr/lib/python2.7 /usr/lib/python3.8 /usr/lib/python3.6
/etc/python2.7 /etc/python /etc/python3.6 /usr/local/bin/python2.7
/usr/local/bin/python3.8 /usr/local/bin/python /usr/local/bin/python2.7-config
/usr/local/lib/python2.7 /usr/local/lib/python3.6 /usr/include/python2.7
/usr/include/python2.7_d /usr/include/python3.6m /usr/include/python3.6
/usr/share/python /usr/share/man/man1/python.1.gz
virtual-machine:~$ python3
//没有报错
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
可见我的python文件是都在的,有可能是PATH没有增加到系统文件中。
由此,我根据网上的各种方法尝试将python链接到python2.7或者3.6上面。但是均未成功。
解决方法
最终我根据 这个链接,修改了别名解决了问题。
virtual-machine:~$ alias python='/usr/bin/python3.6'
virtual-machine:~$ type python
python 是 `/usr/bin/python3.6' 的别名
virtual-machine:~$ python --version
Python 3.6.9
virtual-machine:~$ python
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
至此问题解决了,如果您遭遇了与我同样的问题,本文谨作参考。