5

I'm using Visual Studio Code with the standard Python extension.

My issue is that when I run the code, the Python interpreter instantly closes right after and I only see the output. This means that if I create some data structure I have to create it every single time.

Is it possible to leave the console open after running the code and maybe running multiple files in the same Python interpreter instance?

3
  • It's a general question, for whatever the code I run. If I were to run x = [1,2,3], I want to be able to run print x[0] from another file and still get the result (meaning I want the console not to close after I run the code). Commented Sep 2, 2016 at 20:23
  • Let me try to better explain my problem, I'm using ctrl+shift+b combination to execute my python code from visual studio code. First I run this imgur.com/a/EB8ah and I get the result. Then I run this imgur.com/a/za8am and I get the error that x is not defined, which means the x was not saved in memory, which is I'm guessing because console closes right after every execution. Commented Sep 2, 2016 at 20:36
  • I wrote an answer, after guessing what you wanted. Anyway, even when it seems to you that it is "for whatever the code I run", you should write an example. Because, "whatever the code I run" is sometimes very different from "whatever the code you run", and when we see what you actually ran, it is way easier to understand what you want. Commented Sep 3, 2016 at 22:52

3 Answers 3

3

I used to use spyder which is entirely doing what you want (probably like PyCharm)... Then I briefly tried VS Code, and it is quite easy to make it behave that way too.

First make sure you have an integrated terminal open (or do Ctrl+`, or View > Integrated Terminal), then in that terminal launch ipython. Now, when you use the commands from Don Jayamanne's Python extension (Ctrl+Shift+P to open commands palette):

  • "Run Python File in terminal"
  • "Run select/line in Terminal"

It will run the line inside the ipython console directly. So running the entire file will call python module.py inside ipython, and thus fails.

So to make this work simply create settings to map which command is executed when "Run select/line in terminal":

  • Open Language specific settings (Shift+Ctrl+P, search "Configure Language specific Settings...")
  • Pick up Python
  • Now I would suggest to make change only in your workspace settings (top-right tab) to keep default behaviour in other cases so add in WORKSPACE SETTINGS:

(keep in mind it is just a simple/stupid workaround)

{
    "python.pythonPath": "run"
}

Now when runing whole file, it will use ipython function run within the ipython terminal that we launched, thus keeping all your workspace variables. Also, if you run some line of code with "Run Select/Line in Terminal", the ipython session of that terminal keep all variables.

This allows live debugging, without actually going in the debug mode.

Sign up to request clarification or add additional context in comments.

Comments

1

When you run a program, it runs until it ends. Then it closes. If you want it to stay live longer, you can make a program which does not stop until told so, e.g.

while True:
    something = raw_input('Write something: ')
    print('You wrote: %s' % something)
    if something == 'bye':
        print 'bye.'
        break

This will run until user writes "bye".

1 Comment

That was not the answer I was looking for back then. What I wanted was to know how to run a file in python console and make it so the console remains open after that, same effect if I was to copy paste my file into python console. Seems like VCS didn't have that option, but since then I moved to PyCharm which does.
0

I'm quite late to this conversation, but a workaround I use is to put a pass statement at the end of my file, then add a breakpoint to it. I then run it in the debugger and can access all of the variables etc.

This allows most of the functionality that I used to use in the PyCharm python terminal, such as exploring data structures, checking out methods, etc. Just remember that, if you want to make a multi-line statement (eg. for a loop), you need to use Shift-Enter to go to the next line otherwise it'll try to evaluate it immediately.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.