简体   繁体   English

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

[英]Sublime Text doesn't execute all lines of Python code

Using unregistered version of Sublime Text (is that the issue)? 使用未注册版本的Sublime Text(是问题)吗?

When I run the following code it prompts me for my first name, I enter it and click enter, then nothing happens: 当我运行以下代码时,它会提示我输入我的名字,然后输入并单击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)

However when I run the exact same code in PyCharm it prompts for first and last name 3 times as per the loop, then prints out the resulting dictionary. 但是,当我在PyCharm中运行完全相同的代码时,它会根据循环提示输入名字和姓氏3次,然后打印出结果字典。

I prefer Sublime Text to Pycharm (less bloated) but if it doesn't execute all the code then it probably won't work for me. 我更喜欢Sublime Text,而不是Pycharm(less肿的东西),但是如果它不能执行所有代码,那么它可能对我不起作用。

Any ideas? 有任何想法吗? Is there some setting in Sublime Text I am missing? 我缺少的Sublime Text中是否有某些设置?

The Sublime Text "Build results" pannel (bottom of the interface): Sublime Text“构建结果”面板(界面底部):

在此处输入图片说明

is not interactive, you cannot type input there. 是非交互式的,您不能在此处键入输入。

To solve this, I have added, in addition to the standard CTRL + B build shortcut, another shortcut (in Menu Preferences > Key Bindings - User ): 为了解决这个问题,除了标准的CTRL + B构建快捷方式之外,我还添加了另一个快捷方式(在“菜单首选项”>“按键绑定-用户”中 ):

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

that allows to launch the current file with Python in a new terminal window (there, you can input some data). 这样就可以在新的终端窗口中使用Python启动当前文件(您可以输入一些数据)。

Here is the python_run.py file (to be copied in C:\\Users\\User\\AppData\\Roaming\\Sublime Text 2\\Packages\\User ): 这是python_run.py文件(要复制到C:\\Users\\User\\AppData\\Roaming\\Sublime Text 2\\Packages\\User ):

import sublime
import sublime_plugin
import subprocess

class PythonRunCommand(sublime_plugin.WindowCommand):
    def run(self):
        command = 'cmd /k "C:\Python27\python.exe" %s' % sublime.active_window().active_view().file_name()
        subprocess.Popen(command)

As others have pointed out, Sublime's console does not support input. 正如其他人指出的那样,Sublime的控制台不支持输入。 If you want to run programs which need input from standard input. 如果要运行需要标准输入输入的程序。 You can run it in a GUI terminal. 您可以在GUI终端中运行它。 You can modify Sublime's builtin build system for python and add a variant for Python. 您可以为Python修改Sublime的内置构建系统 ,并为Python添加变体。

  1. In order to modify the builtin python build system. 为了修改内置的python构建系统。 You need to install the package PackageResourceViewer . 您需要安装软件包PackageResourceViewer Follow the guide there to install it. 按照那里的指南进行安装。
  2. After installing PackageResourceViewer, bring up the package control panel by using Shift + Ctrl + P . 安装PackageResourceViewer之后,使用Shift + Ctrl + P来打开软件包控制面板。 Then input prv , and choose Open Resource . 然后输入prv ,然后选择Open Resource 在此处输入图片说明
  3. Then input python , and choose the first item in the result list. 然后输入python ,然后在结果列表中选择第一项。

在此处输入图片说明

  1. In the pop-up panel, choose Python.sublime-build , 在弹出面板中,选择Python.sublime-build 在此处输入图片说明 .

In the opened file, use the following settings: 在打开的文件中,使用以下设置:

{
    "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\""
            }

        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM