如何在 Sublime text 3 中将 python 版本从 2.x 更改为 3.x

deb*_*e4u 0 python python-3.x sublimetext3

目前我已经在 Linux 中安装了 sublime text 3 用于 python 编程。当我执行这段代码时,出现错误,这个错误是因为 sublime 3 使用的 Python 版本是 2.7。如何在 sublime text 3 中将 python 版本从 2.x 更改为最新的 3.x。这是我的一段代码

lis = [2, 1, 3, 5, 4]
# using len() to print length of list
print ("The length of list is : ", end="")
print (len(lis))
# using min() to print minimum element of list
print ("The minimum element of list is : ", end="")
print (min(lis))
# using max() to print maximum element of list
print ("The maximum element of list is : ", end="")
print (max(lis))
Run Code Online (Sandbox Code Playgroud)

当我在 python 2.x 版本上运行这个程序时,我收到一个“无效语法”的错误,但这个错误不是在 python 3.x 版本中出现的。请指导我将sublime text 3的python版本从2.x更改为3.x

Gsk*_*Gsk 7

如果要更改 Sublime Text 中的 Python 版本,首先必须安装两个 Python 版本并知道实际安装它们的位置。
确保通过 cmd 行,您可以使用 python2 和 python3 调用 .py 脚本。
然后,在 sublime text 中,进入Tools --> Build System --> New Build System....
在打开的窗口中,复制粘贴以下代码:

{
    "cmd": ["python3", "-i", "-u", "$file"],
    "file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
    "selector": "source.python"
}
Run Code Online (Sandbox Code Playgroud)

并将此脚本另存为 Python3.sublime-build

您现在应该具有以下值Tools --> Build System

Python # which is your Python2
Python3 # which is your Python3
Run Code Online (Sandbox Code Playgroud)

注意我们写的地方"python3",如果那不行,你可以把你的python3安装路径,比如"/usr/bin/python3"

本讨论的末尾查看一些提示。