Ubuntu Python2 和 Python3 共存 切换

mac2022-06-30  73

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

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

转载于:https://www.cnblogs.com/vercont/p/10210222.html

相关资源:Ubuntu下Python2与Python3的共存问题
最新回复(0)