24

I'm using visual studio code with DonJayamanne python extension. It's working fine but I want to have an interactive session just like the one in Matlab, where after code execution every definition and computational result remains and accessible in the console.

For example after running this code:

a = 1

the python session is terminated and I cannot type in the console something like:

b = a + 1
print(b)

I'm aware that the python session can stay alive with a "-i" flag. But this simply doesn't work.

Also every time I run a code file, a new python process is spawned. Is there a way to run consecutive runs in just one console? Again like Matlab?

This sounds really essential and trivial to me. Am I missing something big here that I can't find a solution for this?

user1
  • 601
  • 1
  • 6
  • 12

3 Answers3

16

I'm the author of the extension. There are two options:

  1. Use the integrated terminal window (I guess you already knew this)
    Launch the terminal window and type in python.
    Every statement executed in the REPL is within the same session.

  2. Next version will add support for Jupyter.
    Please have a look here for some samples of what is yet to come:

Don
  • 6,632
  • 3
  • 26
  • 34
  • 1
    Nice, but this is not what I asked for. If I run a python file from within the ide, it runs in a different terminal. I don't want to open a terminal manually and load the same file with a long command every time. Ideally there should be a run in the same ipython session command which runs: runfile($file, wdir=$dir) in the current running ipython session and does not spawn a new ipython (or python) – user1 Oct 04 '16 at 18:44
  • 1
    If that's what you want, you'll probably have to wait for the next release to go with Option 2 (this uses the same ipython session every time). And the possibility of running a file within the same terminal will be added later (currently this is not possible as we cannot determine whether the user has closed the terminal or not), I believe an API for this will be provided in the VS Code release. – Don Oct 04 '16 at 21:38
  • 1
    Using the "Run file in Terminal" or "Run Select/Line in Terminal" (with F5 and F9 key for Matlab-like behaviour) prompts an integrated terminal (called 'Python' by default) with @Don 's extension. I usually start from here, quit (ctrl+D), (then `source activate ..` some conda env if necessary) then launch `ipython` from here. Finally the only thing to change is to add in your workspace settings e.g. "python.pythonPath": "run", this only to be able to run whole file in ipython with F5 (the Run file in Terminal command...). With some tweaks with launch.json, I am sure it could be better though. – H. Rev. May 16 '17 at 10:52
  • @H.Rev. I tried your solution of adding this line "python.pythonPath": "run" to the user settings in vscode. Still the Run file in Terminal command runs using the regular python and doesnt send it to the open ipython terminal. Is there something I am missing? – skr Nov 29 '17 at 19:29
  • You're ipython terminal is labelled "Python"? If it still the "bash" one it won't send it there indeed... What happens if after it sends to the regular python terminal you quit() python and launch ipython from the same console, and then press F5? (btw if you have python.pythonPath: run, then it should try to do "run filename.py", which is python will give a syntax error, is that what you get?) Also make sure your setting is not overwritten by local workspace settings. – H. Rev. Nov 30 '17 at 17:16
6

I added the following lines into user setting file, then it works. Select some lines of python code, then right-click and select Run selected code in python terminal

Solution 1: Will start iPython terminal

   "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
   "terminal.integrated.shellArgs.windows": ["/K ipython"],

Solution 2: Will start a terminal like "python -i"

   "python.terminal.launchArgs": ["-i"],
LightCC
  • 9,804
  • 5
  • 52
  • 92
Chaos2020
  • 61
  • 1
  • 2
3

The following line will solve your problem.

 "python.terminal.launchArgs": ["-c","\"from IPython import embed; embed()\""]
nirualapm
  • 31
  • 2