Ubuntu Python2 和 Python3 共存 切换

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DarrenXf/article/details/82948154

例如 你写了代码
创建一个文件 在终端

vim test.py

然后写入代码

print “hello world"

接着运行代码

python test.py

会输出

hello world

这个就是系统默认用了python2 来运行python代码
如果想用python3来解释这个代码文件
可以这样运行

python3 test.py

但是我们在文件里的写的代码是不能在python3的解释器里运行成功的
会输出

 File "test.py", line 12
    print "hello world"
                      ^
SyntaxError: Missing parentheses in call to 'print'

这是因为print函数在python2和python3中的语法不同
需要把这个函数修改为

print("hello world")

这样再次运行

python3 test.py

输出

hello world

猜你喜欢

转载自blog.csdn.net/DarrenXf/article/details/82948154