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)
Related
I'm writing some python to render stuff that I tweak and run a lot, and that runs inside a virtual env. I would like a keyboard command to run a bash script (that launches python) inside the known terminal and virtual env.
I played a bit with setting up a shell script and a custom task, but entering the virtual env is always a bit tricky.
I don't need a debugger or anything complicated, just a way to run the python code and attach a keystroke to it.
https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
https://code.visualstudio.com/docs/python/jupyter-support-py
You can just run the python.exe/python binary in the virtualenv folder as an executable.
So to run a specific python file with a virtual environment:
For Windows it would be .\path_to_virtualenv\Scripts\python.exe yourfile.py, and for Unix system it would be ./path_to_virtualenv/bin/python yourfile.py
And instead of running a python file, you can probably pipe the input command into the python executable in the path above if you want to run a specific python command.
So something like COMMAND | ./path_to_virtualenv/bin/python yourfile.py
Could you please explain the difference between the way you want and the following operation:
Use "Ctrl+Shift+P" and type "Python: Select Interpreter" (the same as click the interpreter in the lower right corner which is on the right side of python).
Use "Run Python".
I think there is no difference because before your using a keyboard command, you still need to choose the interpreter.
By the way, use jupyter notebook or interactive window will be a good chioce as well.
I am using PyCharm 2021.2.4 on macOS Big Sur 11.6 with my interpreter running Python 3.9
My PyCharm debugger console displays as read only without the command prompt. I need the prompt so I can set the proper variables when I launch the script.
Here's what my debugger console looks like:
Here's the prompt I'm expecting as per the Jet Brains documentation: https://www.jetbrains.com/help/pycharm/using-debug-console.html#open
So far, I have tried:
Verifying that "Emulate terminal in console output" is deactivated as per: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000244824-Console-toolbar-is-gone?page=1#community_comment_115000347810
Installing PyCharm 2020.3 to check whether an earlier version exhibits the same behavior (it does).
Creating a conda environment using Python 2.7 as the interpreter.
Resetting the IDE to its default settings.
And in case it's relevant, I'm trying to run construal_level_task.py which is the file in this repo https://github.com/UOSAN/high_level_construal
At the command line, the file is launched with this command: python3 construal_level_task.py --id ASH999 --run 1 --session 1 and those inputs (id, run, session) are the ones I'm trying to enter in the debugger console to run the script in debug mode.
The python program exited, so you cannot interact with the console any more. You need to set a break point in your code. Then the debug console will become interactive.
those inputs (id, run, session) are the ones I'm trying to enter in the debugger console
Using the debug console is not the right way to enter inputs from command line arguments. Instead, you should edit the run configuration and add them there. See Pycharm and sys.argv arguments for an explanation of how to do this.
I think the problem is that you use conda interpreter. Is there a need for this? You can try using pure interpretator.
EDIT: Try to use virtual using normal interpreter (not conda). Your mistake is conflicting copies of conda envs.
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.
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!