I'm wondering if there is a way to setup VS Code to debug like you can in Spyder or closer to a Jupyter or ipython notebook where you can run some block or line of code (with F9 in Spyder) and that code is interpreted and the debugging session is still 'live' if you will.
In VS code I can't seem to run any block of code and then run another block of code in the same session without stopping and starting up another debugging session.
I tend to like to load a bunch of data and then execute arbitrary lines of code after that as i figure out what i want to do with it.
There is an option in the command palette to do this. With your python code open select lines you want to run. Then go to view menu and select command palette. There is a command that says, Python: Run selection/line in Python terminal. This will run just what you select and print out any outputs.
This has been added to VS Code:
https://code.visualstudio.com/docs/python/jupyter-support
Related
I used to use Pyzo for Python coding and decided to give VS Code a try because it is more feature-rich. I came across one huge annoyance, however. In Pyzo, I am used to code „interactively“ as Pyzo executes code in an interactive shell (https://pyzo.org/features.html).
I would like to replicate that in VS Code, but so far had no luck. With the Microsoft Python extension installed, the closest I can come is to select the whole code, right click and then click on „Run Selection/Line in Python Terminal“. For long scripts, however, this is very, very slow as it first prints each line to the terminal and then executes it line by line. Pyzo seems to operate silently.
Do you have a solution? I think, VS Code would be much faster if it did not print each line to the terminal first.
Best
I use Python Interactive window and find it very useful. It is not an immediate REPL but gives you the time to write a block of code and execute it with a key stroke.
At the bottom of the interactive pane you have a command line where you can type like a REPL. It is a temporary cell (multi line)
Use Code Runner(extension available in the marketplace of VS Code) or run a bash script in the terminal (python -u [name of the file])
I'd like to send code I have selected from the editor to the interpreter running in the debugging console during an active debugging session. Can I do this in VSCode? If so, how?
Update 1
While Mark (accepted answer) provided what seems to be the right command (it works for me from the contextual menu with the mouse), this isn't working for me yet as a keyboard binding, and I reported this issue here.
Update 2
This started working again as of April 21, 2020 (latest Insiders version).
See https://code.visualstudio.com/docs/python/editing#_run-selectionline-in-terminal-repl
Run Selection/Line in Terminal (REPL)
The Python: Run Selection/Line in Python Terminal command
(Shift+Enter) is a simple way to take whatever code is selected, or
the code on the current line if there is no selection, and run it in
the Python Terminal. An identical Run Selection/Line in Python
Terminal command is also available on the context menu for a selection
in the editor.
VS Code automatically removes indents based on the first non-empty
line of the selection, shifting all other lines left accordingly.
Source code that runs in the terminal/REPL is cumulative until the
current instance of the terminal is closed.
The command opens the Python Terminal if necessary; you can also open
the interactive REPL environment directly using the Python: Start REPL
command. (Initial startup might take a few moments especially if the
first statement you run is an import.)
On first use of the Python: Run Selection/Line in Python Terminal
command, VS Code may send the text to the REPL before that environment
is ready, in which case the selection or line is not run. If you
encounter this behavior, try the command again when the REPL has
finished loading.
And see Use IPython REPL in VS Code for info about the IPython REPL.
--------- generic info for other languages -------------------------------------------------------------
It sounds like you want to send it the repl. There is an unbound command:
editor.debug.action.selectionToRepl
which will send selected text to the debug repl.
{
"key": "alt+y", // whatever you want here
"command": "editor.debug.action.selectionToRepl"
},
No, you cannot send a selection of code to the debugger. You need to debug the whole file or use the Interactive Window support to debug a specific cell.
Currently, in Visual Studio Code (under Windows 10 64bits), at a Python file called path\myfile.py, if one clicks with mouse right-button for context menu and then chooses 'Run Python File in Terminal', an integrated CMD terminal is open and file is automatically run there with:
python.exe path\myfile.py
After the file stops running, one is naturally left at the integrated CMD cursor.
This behavior is quite different, for instance, from what one has with an IDE like Spyder. There, when you run code (e.g. with F5), at the end one is left still at the Python cursor and can access content of variables created when code was run.
Is there a way to achieve a similar behavior in Visual Studio Code?
You can configure VS Code Python extension to use the -i command line option
Described in https://docs.python.org/3/using/cmdline.html#cmdoption-i
You only have to add the setting bellow (inside settings.json file)
"python.terminal.launchArgs": ["-i"],
This will execute the command python.exe -i path\myfile.py.
I don't know if it is a new feature, but I've been using it for while.
If you would like to use the terminal IPython, like in Spyder, you can use a different set of options, as the following:
"python.terminal.launchArgs": ["-m","IPython","-i"],
With these, VS Code will execute the command python.exe -m IPython -i path\myfile.py.
Then, it will run IPython module as a "script" (with -m option), which will use the options -i path\myfile.py, i.e., IPython will run the file and remain opened.
BTW, another thing is: you can run "cells" in Spyder's integrarted terminal (regions of code with #%%). But in VS Code it seems you can't.
I've made a question with a "work around" to run cells of Python files in VS Code Integrated terminal, which is posted Here
Yes. Open a terminal window and it's like a terminal window on your computer. You can type python filepathandname and the python script will execute like it does from the command line.
The closest you can come is to run the code under the debugger and set a breakpoint at the end to pause the exiting of the execution. Otherwise feel free to file a feature request at https://github.com/microsoft/vscode-python.
I would like to know if there is way to figure out (from the IPython console) the .py script that was used to run/execute the python commands interactively and thus got printed into the Ipython console.
For eg.
From the below screenshot, looking at the 3+3 in the Ipython console, I can see that it came in when command from untitled2.py was executed.
However when the scripts get long, and IPython output gets long and you are often shifting scripts on the left side, it can be hard to keep track.
So i was wondering if there is a way i can quickly execute some command or view some setting on Ipython console that can tell me that the above line came from untitled.py.
(Spyder maintainer here) There's no way to know from which Editor you're executing some portion of code, sorry.
However, you could use dedicated consoles (under Preferences > Run > Execute in a dedicated console) to have one console per file you execute, as long as you use F5 to run each one of them.
You could edit each .py script, so the first thing it does is to print its name and functionality.
untitled2.py:
print('untitled2.py')
...
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?
I'm the author of the extension.
There are two options:
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.
Next version will add support for Jupyter.
Please have a look here for some samples of what is yet to come:
#303
Screen samples and more
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"],
The following line will solve your problem.
"python.terminal.launchArgs": ["-c","\"from IPython import embed; embed()\""]