Sublime Text不执行Python代码的所有行

2024-06-28 14:22:05 发布

您现在位置:Python中文网/ 问答频道 /正文

使用未注册版本的崇高文本(这是问题吗)?在

当我运行以下代码时,它会提示我输入我的名字,我输入它并单击enter,然后什么都不会发生:

dict_1 = []
count = 0

while count < 3:
    fn = input('What is your first name:')
    ln = input('What is your last name:')
    dict_1.append({
        "first_name": fn,
        "last_name": ln
        })
    count += 1

print(dict_1)

但是,当我在PyCharm中运行完全相同的代码时,它会按照循环提示输入3次名字和姓氏,然后打印出结果字典。在

我更喜欢崇高的文本而不是Pycharm(不那么臃肿),但是如果它不能执行所有的代码,那么它可能就不适合我了。在

有什么想法吗?在崇高的文本中有没有我所缺少的背景?在


Tags: 代码name文本inputyouriscount名字
2条回答

正如其他人所指出的,Sublime的控制台不支持输入。如果要运行需要从标准输入输入的程序。你可以在GUI终端上运行它。您可以为python修改Sublime的内置build system,并为python添加一个变体。在

  1. 为了修改内置的python构建系统。您需要安装包PackageResourceViewer。按照那里的指南进行安装。在
  2. 安装PackageSourceViewer后,使用Shift+Ctrl+P打开包控制面板。然后输入prv,并选择Open Resourceenter image description here
  3. 然后输入python,并选择结果列表中的第一项。在

enter image description here

  1. 在弹出面板中,选择Python.sublime-buildenter image description here。在

在打开的文件中,使用以下设置:

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

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "python -m py_compile \"${file}\"",
        },

        {
            "name": "Run in console",

            "windows":{
                "shell_cmd": "start cmd /k python -u \"$file\""
            },
            "linux":{
                "shell_cmd": "xterm -hold -e python -u \"$file\""
            },
             "osx":{
                "shell_cmd": "xterm -hold -e python -u \"$file\""
            }

        }
    ]
}

崇高文本“构建结果”面板(界面底部):

enter image description here

不是交互式的,不能在那里输入。在

为了解决这个问题,除了标准的CTRL+B构建快捷方式之外,我还添加了另一个快捷方式(在菜单Preferences>;Key Bindings-User中):

{ "keys": ["ctrl+shift+alt+b"], "command": "python_run" }

它允许在一个新的终端窗口中用Python启动当前文件(在那里,您可以输入一些数据)。在

以下是python_run.py文件(要复制到C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User)中:

^{pr2}$

相关问题 更多 >