I am confused with the Run Selection/Line in Python Terminal command. In my VsCode (Windows 11). I have two commands with the same shortcut Shift+Enter.
One is launching an Interactive Jupyter notebook (awful). The other sends it to a Python Terminal (better). My ideal case would be to send it to an Ipython terminal.
How to configure that properly?
Could you try this?
"terminal.integrated.defaultProfile.windows": "IPython",
"IPython": {
"path": "C:\\Work\\python3.10\\.venv\\Scripts\\ipython.exe" //The path to the ipython.exe, please take care of which ipython you have selected, this means which python interpreter selected in the terminal.
},
Related
I was trying to change the executer of python in vscode.
I have installed python3 in my system.However default executer of python in vscode is python.So i am getting error as 'python not found' whenever i try to run code. So how could i change the executer to python3 in line no 4066?
I tried to change from user settings. I could change the interpreter there. However i didnt find any way to change the executer.
That's default command Code Runner provides for Python Language.
As long as the execution scripts in terminal when you run python file is python3.8, run code should also use python3.8.
OR you can change the command directly:
"code-runner.executorMap": {
"python": "path/to/python3.8/python.exe -u",
}
I wanted to use IPython interactive in VSC (not using a notebook). By using a keybiding for workbench.action.terminal.runSelectedText, I can successfully execute a selection to a python interactive shell, but with iPython it fails to run the cell and I have to change selection to the terminal and press enter.
Here's my keybidings.json file:
[
{
"key": "ctrl+enter",
"command": "workbench.action.terminal.runSelectedText"
}
]
Any help to solve this is much appreciated! Ideally, a keybinding configuration that includes an extra enter.
I have this problem when I upgraded IPython to version 7.30.1
The solution I found is that add "--simple-prompt", this is not perfect (just no color theme or auto completion), but at very least, you don't need to press enter when sending codes to run.
IPython is not officially supported by the Python extension and this is part of the reason: IPython's design simply requires you to press Enter an extra time based on how VS Code sends text into the terminal.
This is an issue with IPython's autoindent feature. You can disable this by passing --no-autoindent when launching IPython (i.e. ipython --no-autoindent).
edit: added gif.
I was annoyed by this quirk in vs-code too.
I noticed that when using a python virtual environment (I use pipenv) my selected line was executed in an Ipython terminal and no extra Enter key press was needed.
To reproduce or test out:
Launch VS Code
Open a python file and a terminal window
In the terminal run pipenv shell (requires pip install pipenv && pipenv install ipython)
Then in the terminal run ipython (or ipython --no-autoindent)
Select a line in your python file and from a vs-code command palette execute Terminal: Run Selected Text in Active Terminal (or use a keyboard shortcut)
How can I choose ipython/jupyter as the DEFAULT python terminal? I use both a windows 10 and a linux machine with the anaconda distribution.
If I type "ipython" on the terminal, it opens an ipython session. If I run the debugger or shift+enter a line, it automatically runs on a "barebones" python shell. Should be simple...but I have been googling and messing with the settings for half an hour with no success.
Looked up
https://code.visualstudio.com/docs/python/tutorial-flask
Use IPython REPL in VS Code
but could not find a way to set it up on my linux or win10 machines. Any ideas?
A slightly neater way to achieve #TwoUnderscorez's answer is to just launch the module with -m IPython:
"python.terminal.launchArgs": [
"-m",
"IPython"
]
Edit: For anyone struggling with IndentationError: unexpected indent errors, try the following:
"python.terminal.launchArgs": [
"-m",
"IPython",
"--no-autoindent",
]
(wouldn't have just added a comment to the existing answer, but not enough rep)
In your VSCode, press ctrl+shift+P, start typing settings and click on Preferences: Open Settings (JSON)
Add this key-value pair to tell python to start ipython:
"python.terminal.launchArgs": [
"-c",
"\"from IPython import start_ipython; start_ipython()\""
]
There currently isn't support to specify an alternative REPL that isn't the Python interpreter you use to execute code. One trick some people do if you want this just for sending code to the REPL is they launch the REPL once, exit it, and then launch ipython manually as the extension will continue to use that terminal instance for future code sent to the REPL.
Using the Python extension of Visual Studio Code, I can select some code, right-click it, and select "Run Selection/Line in Python Terminal" (alternatively, I can hit Shift+Enter). However, this sends the selected code to a plain old Python REPL in the Terminal pane, whereas I'd like to have this code run in IPython instead (not the QtConsole, just the terminal-based IPython).
Is it possible to set IPython as the default REPL? I tried setting /usr/local/bin/ipython3 as my default Python environment, but that doesn't work (it still executes the plain Python interpreter). FWIW, I'm on macOS.
Adding the following setting (Preference: Open Settings JSON; or Preference -> Settings -> Search launchArgs -> edit in json) works without any extension. It also fixes the issue that multiple lines cannot be sent to Python.
"python.terminal.launchArgs": [
"-c",
"\"import subprocess; subprocess.call(['ipython', '--no-autoindent'])\""
],
Update (2020-12-27): the following setting seems to work better because it supports Ctrl+C keyboard interrupt without existing IPython:
"python.terminal.launchArgs": [
"-m",
"IPython",
"--no-autoindent",
],
Type Ipython inside the terminal window. Then select the line or lines you want to run from the editor window and then click on the Terminal menu at the top of VScode window. One option in the Terminal menu is to "Run Selected Text". This will be run in the Ipython terminal window. I don't know how to make this the default but it appears to remain in that state unless Ipython is stopped. Note: You have to run your selections using the Menu item. Right-clicking in the editor window and clicking on "Run Selection" will not use the Ipython window. I hope this is clear. If not just drop a comment.
Use "IPython for VSCode" plugin.
Install it and then use Send Select Text (or current line) To IPython
If you want use shortcut setting with original shift+enter to execute command above, Use One of below methods.
Shortcut setting - Normal
open shortcut setting: Macos it's cmd+k cmd+s.
search command above and right click to modify the keyboard binding as shift+enter.
Next, right click again to modify the When expression as:
editorTextFocus && !findInputFocussed && !python.datascience.ownsSelection && !replaceInputFocussed && editorLangId == 'python'
Right click and select show same key bindings
Find command Python: Run Selection/Line in Python Terminal and Right click to disable it.
Shortcut setting - JSON
Open shortcut setting and click Upper right corner to open JSON config
Append these settings:
{
"key": "shift+enter",
"command": "ipython.sendSelectedToIPython",
"when": "editorTextFocus && !findInputFocussed && !python.datascience.ownsSelection && !replaceInputFocussed && editorLangId == 'python'"
},
{
"key": "shift+enter",
"command": "-python.execSelectionInTerminal",
"when": "editorTextFocus && !findInputFocussed && !python.datascience.ownsSelection && !replaceInputFocussed && editorLangId == 'python'"
}
I start IPython from inside the standard Python REPL that's spawned by Shift-Enter with
import IPython
IPython.embed()
See IPython docs.
You could also set the "python.pythonPath" in your settings.json as follows:
{
"python.pythonPath": "~/miniconda3/bin/ipython3",
"python.dataScience.sendSelectionToInteractiveWindow": false
}
or
{
"python.pythonPath": "~/miniconda3/envs/<yourEnv>/bin/ipython3",
"python.dataScience.sendSelectionToInteractiveWindow": false
}
shift+enter will then trigger ipython and send the line to the terminal.
IPython support is provided by "IPython for VSCode" plugin.
Just select the text and invoke 'Send Selected Text (or current line) To IPython' in command palette.
Also official Microsoft Python plugin now supports interactive Jupiter windows, with similar functionality.
How to create a Jupiter Notebook in VS Code
Go to the command palette (Command + Shift + P)
Search for: "Jupyter: Create New Blank Notebook", and hit enter
If you have a default vanilla installation of the Python extension in VSCode, by default you can highlight python code and do "SHIFT+ENTER" to "Run Selection/Line in Python Terminal".
That command will use the default python.exe interpreter. However, this is the trick that works for me to use the IPython shell instead.
First run a dummy line of python code by highlighting it and doing SHIFT+ENTER. This launches a terminal named "python" and starts the python shell to run the code in the REPL.
Now issue exit() in that python shell to return to the regular terminal prompt.
Run ipython in that terminal to start the IPython REPL where the plain old Python REPL used to be.
Now subsequent uses of SHIFT+ENTER (single or multiple lines highlighted) will run the code in the IPython shell.
(Note, if SHIFT+ENTER is sending code to the Python Interactive split window instead of a terminal REPL, make sure your settings.json has "jupyter.sendSelectionToInteractiveWindow": false,)
[EDIT]. Various comments on this thread remark that using the various solutions, code is copied to the IPython terminal, but not run. I realized I also have this experience depending on my active conda environment. If I do conda install -c conda-forge prompt-toolkit in the environment I'm using, I get the expected behavior where SHIFT+ENTER actually runs code. I don't know if that package is the key, one of its dependencies, or just using conda-forge. But it works!
In Matlab and R, I can run a code and the console/terminal of MATLAB's editor and R-Studio will expose the session to me. In a way that I can access all the variables and results of my simulations from the console. I have found some solutions online, but I am not satisfied with them or they were not clear to me.
So here is the question:
How can I modify VS Code so that the code which I am running will have the same session in the terminal or change it to pythons currently running session?
ipython or jupyter seems to do this task. How can I do that in ipython/jupyter?
Let say if I have in my python p1.py the following line:
a=3
and I execute the above-mentioned line, after execution, I want to be able to input the following line in the terminal or console of VS Code and get a correct result:
b=a+10
Many thanks!
You can use Run Selection/Line in Python Terminal which will send the text to a Python REPL instance. If you're after changes in the REPL to be reflected in the editor then I'm afraid that support does not exist.
In USER SETTINGS I modified the following:
{
"jupyter.appendResults": true,
//"terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"terminal.integrated.shell.windows": "C:\\Program Files\\Python36\\Scripts\\ipython.exe",
"[python]": {
}
}
Then I added this line:
{
"python.pythonPath": "run"
//"python.pythonPath": "C:\\Program Files\\Python36\\python.exe"
}
In the WORKSPACE SETTINGS of VS Code. After running a simple addition/subtraction test, I can access the variables in the ipython terminal.