Reputation: 3801
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
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.
prv
, and choose Open Resource
.
python
, and choose the first item in the result list.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
Reputation: 46371
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)
Upvotes: 3