简体   繁体   中英

Sublime Text doesn't execute all lines of Python code

Using unregistered version of Sublime Text (is that the issue)?

When I run the following code it prompts me for my first name, I enter it and click enter, then nothing happens:

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.

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.

Any ideas? Is there some setting in Sublime Text I am missing?

The Sublime Text "Build results" pannel (bottom of the interface):

在此处输入图片说明

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 ):

{ "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).

Here is the python_run.py file (to be copied in 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. If you want to run programs which need input from standard input. You can run it in a GUI terminal. You can modify Sublime's builtin build system for python and add a variant for Python.

  1. In order to modify the builtin python build system. You need to install the package PackageResourceViewer . Follow the guide there to install it.
  2. After installing PackageResourceViewer, bring up the package control panel by using Shift + Ctrl + P . Then input prv , and choose Open Resource . 在此处输入图片说明
  3. Then input python , and choose the first item in the result list.

在此处输入图片说明

  1. In the pop-up panel, choose 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\""
            }

        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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