0

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?

JD2775
  • 3,658
  • 7
  • 30
  • 52
  • 1
    Do u have python 3 in sublime or are you running on python 2 –  Apr 22 '18 at 23:32
  • @BOi . Python 3, I think? Otherwise it wouldn't have run at all I am guessing because of the print() with parentheses, also the input vs raw_input – JD2775 Apr 22 '18 at 23:33
  • 2
    Possible duplicate of [Sublime Text 2 console input](https://stackoverflow.com/questions/10604409/sublime-text-2-console-input) – dfundako Apr 22 '18 at 23:34
  • 1
    If you're using python 2 to run it on sublime text it doesn't know what input is. So it wont ask you for one –  Apr 22 '18 at 23:34
  • 1
    @dfundako exactly –  Apr 22 '18 at 23:35
  • 1
    @BOi I found that link by Googling "Sublime text python input" – dfundako Apr 22 '18 at 23:37
  • 2
    @JD2775 please read the link. It should solve your question. Next time make sure you google your questions before asking it here. We don't want to keep answering duplicates :) –  Apr 22 '18 at 23:39

2 Answers2

3

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)
Basj
  • 41,386
  • 99
  • 383
  • 673
0

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

        }
    ]
}
Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
jdhao
  • 24,001
  • 18
  • 134
  • 273