Sublime Forum

Problem Getting Started

#1

I am trying to run the following code as my test with my new install of sublime text 3
a = “This is my string”
len(a)

I do not see any program failure messages, BUT I do not see the expected answer of 17.

If I run this code…
def hello(name):
print(’ Hello ’ + name)

// I get the output correctly below

hello(‘Alice’)
hello(‘Bob’)

0 Likes

#2

This is because your program calculates the length of the string with len, but doesn’t do anything to display it.

Python is capable of:

  • Running the program you provide it from an input file, then exiting
  • Running the program you provide it from an input file, then entering interactive mode to allow you to continue entering statements
  • Running directly in interactive mode for you to enter statements

When running interactively, it acts as a REPL (Read-Evaluate-Print-Loop), where you enter a statement, it executes it, then displays the result and waits for more input.

Possibly the tutorial/book that you’re using to learn Python is showing examples of code as executed in the REPL mode.

Sublime uses the first method above; it tells the Python interpreter to run your program and then exit. In this mode the interpreter only does exactly what you told it to. As such, since your program doesn’t display the result of the operation, it appears as if nothing happens.

You can modify your program to print the output:

a = "This is my string"
print(len(a))

In general, Sublime can’t run in the other two modes, because although the output of the interpreter is connected to the output panel so you can see what’s happening, it doesn’t connect the input of the interpreter to anything; thus if you run a program that waits for user input, your program will block forever because they’re no way to pass it the input.

You can get around that by using a REPL plugin for Sublime, which gives you the interactive mode you may expect. One such example is SublimeREPL, which seems quite popular although I have no experience with it personally.

1 Like

#3

Currently you cannot run the python in REPL mode within Sublime Text, except for the SublimeREPL package. So you cannot interact with your script after it is running. There is these feature requests for Sublime Text which might interest you, so you could up vote them:

  1. Core$478 Allow for user interaction when running programs in the Build Panel
  2. Core$1468 Full/complete terminal features on Sublime Text
0 Likes

#4

Thanks to you both.

0 Likes