Sublime Forum

Simple Python script gets stuck

#1

I just got sublime text 3 for Ubuntu. I have no experience in either. I have this very simple python program that I am trying to run to see if everything works properly. The first line asks for input from the user and when I run the script and type the imput, it just stays there, it doesnt go to the next line, and if it does, I cant see it.

name = input("What’s your name? ")
print("Nice to meet you " + name + “!”)
age = input("Your age? ")
print(“So, you are already” + age + " years old, " + name + “!”)

Thank you so much.

0 Likes

Build broken? - Python 3 - input()
#2

It’s not possible with core Sublime to run an interactive program such as this directly within Sublime. The build process is more for running programs than interacting with them, so there’s nothing that connects the input of your program to anything in Sublime.

The net result is that once your program hits the input, it’s effectively hung forever waiting for you to provide input that you can’t give it.

One solution is to use use SublimeREPL to run interactive python scripts from directly within Sublime, although this requires some setup.

Alternatively, you can select Tools > Build System > New Build System..., then replace the contents of the buffer with the following and save it as "Xterm_Python.sublime-build` (or some similar name):

{
    "shell_cmd": "xterm -e 'python -u \"$file\" ; echo \"<RUN COMPLETE>\" && read'",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "env": {"PYTHONIOENCODING": "utf-8"},
}

Once it’s saved, go to Tools > Build System and choose the build; it will appear in the menu with the same name as you gave the file above.

Now when you build, an external xterm window will be opened and the program will run in there. When it’s finished, press Enter to close the xterm window and return to Sublime.

1 Like

#3

I assume that you are using the Sublime Text build system to run the program. If so, the build system cannot handle input statements. You will have to use Sublime REPL ( package you would install with Package Control) to use Python input statements in Sublime. Or use IDLE or some other program. Why not start with a “Hello World” program that requires no input?

0 Likes

#4

You can use the built in Sublime functions to request input from the user…
https://www.sublimetext.com/docs/3/api_reference.html

Sublime Window Class

View Shows the input panel, to collect a line of input from the user. on_done and on_change, if not None, should both be functions that expect a single string argument. on_cancel should be a function that expects no arguments. The view used for the input widget is returned.

show_input_panel(caption, initial_text, on_done, on_change, on_cancel)	

so you can run sublime.active_window( ).show_input_panel( ‘The question to ask?’, ‘Default text in the question line…’, None, None, None )

The on done is for a function / callback when the user hits enter… on change is when the text changes ( so you can demand numbers only most likely by refusing input - this is a typical functionality for an on change callback, and on cancel is called when the user hits escape or doesn’t hit enter…

This creates a way to enter text at the bottom of the screen - a lot of plugins use it to accept data… I believe the Package Control plugin does too - for repo entries, etc…

Hopefully this helps.

0 Likes

#5

Interesting, I did what you said and the xterm window does not pop up.

0 Likes

#6

Hmm… do you see any errors generated at all?

0 Likes

#7

It’s not a window but a subtle input box at the bottom - similar to the box which shows up when you use the simple ctrl + f / find in current file… Not the find in files triple decker but the single…

The same system seems to be used for find, add package control repo, etc…

0 Likes