JD2775
JD2775

Reputation: 3801

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?

Upvotes: 0

Views: 2696

Answers (2)

jdhao
jdhao

Reputation: 28329

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. enter image description here
  3. Then input python, and choose the first item in the result list.

enter image description here

  1. In the pop-up panel, choose Python.sublime-build, enter image description here.

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

        }
    ]
}

Upvotes: 0

Basj
Basj

Reputation: 46371

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

enter image description here

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)

Upvotes: 3

Related Questions