【问题标题】:How to change python version from 2.x to 3.x in Sublime text 3 [duplicate]如何在 Sublime text 3 中将 python 版本从 2.x 更改为 3.x [重复]
【发布时间】:2018-06-27 11:44:49
【问题描述】:

目前我已经在 Linux 中安装了 sublime text 3 用于 python 编程。 当我执行这段代码时,我收到错误,这个错误是因为 Python 版本 sublime 3 使用的是 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))

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

【问题讨论】:

  • *澄清:我在这里收到错误消息:打印(“列表的长度是:”,结束=“”)
  • 您能否指定您的错误消息是什么,以确保解决正确的问题?
  • 要在python2中工作,请使用print "The minimum element of list is : {}".format(min(ls))
  • 您是在问如何解决python2中的问题或如何在sublime text中更改python版本?因为您的问题和接受的答案不匹配
  • 我想在 sublime text 中更改 python 版本。

标签: python python-3.x sublimetext3


【解决方案1】:

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

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

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

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

Python # which is your Python2
Python3 # which is your Python3

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

查看this discussion末尾的一些提示。

【讨论】:

    【解决方案2】:

    this 问题的重复。

    你不能在python 2中运行语句print('something: ', end='')。原因是print是Python2中的文字,不能接受参数。而在 Python3 中,print 是一个函数。

    为了在 Python2 中打印类似的东西,你可以使用这个语法 -

    print "The length of list is : ",

    注意语句末尾的逗号。

    Source

    【讨论】:

    • 如果这解决了您的疑问,请投票或将其标记为答案。迫切需要一些声誉。 :P
    • 知道了。欣赏。
    猜你喜欢
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2013-08-27
    • 1970-01-01
    相关资源
    最近更新 更多