Sublime Forum

How to link a script with the console?

#1

Hi,
I would like to do what I could do in Idle or Matlab
After running a script, I would type for example “counter” or “i” in the console and I would get the answer “43” or “99”, meaning that it’s the last value when the script ended for thoses variables.

With using python in sublime text 3 , when I do that in the console, the variable isn’t recognised.

I think it’s very useful and basic. Could it be done ?
Thanks

0 Likes

#2

The console in Sublime is the console for the internal Python interpreter that’s used to run the plugin system. It is in every way divorced from any version of Python that you might have installed on your computer (including if you have no Python installed at all).

To do something like what you want you need to be able to run an external Python program and then tell the interpreter to go interactive at the end. You can’t run interactive programs directly within Sublime because the input that you type in the output panel doesn’t get sent back to the running program; hence any program that asks for input will hang indefinitely because you have no way to talk to it.

The general solution is to use something like SublimeREPL or Terminus to make a build system that runs the script and then goes interactive at the end allowing you to interact with the program. My general recommendation would be to use Terminus for something like this.

There is a video excerpt that goes over how you would create a sublime-build that uses Terminus to run an interactive program. In your case the resulting sublime-build file would look the one below.

This playlist contains several videos that talk about how Build Systems work in general if you’re unfamiliar with the concept. The excerpt linked above is from an upcoming video on this particular topic.

{
  "target": "terminus_open",

  "cmd": ["python", "-i", "-u", "${file}"],
  "working_dir": "${file_path}",

  "auto_close": true,

  "selector": "source.python"
}
0 Likes

#3

Thanks OdatNurd for your help.
I’ve installed sublimeREPL,
it works well.

0 Likes