ipdb works fine in the shell, but I want to debug under vim, after I set ipdb.set_trace(), and then !python %,
the console below gives me this messy prompt, any idea?
I guess you are using a GUI Vim. GVim? MacVim? The pseudo terminal you get when executing external tools is not, has never been and will probably never be able to understand the escape characters you see. That means no color and no ncurses-style widgets.
You'd better run it in a separate terminal or find a way to disable colors in iPython.
If you don’t really want to patch vim as well as run in a separate terminal as #romainl suggests then there is Conque plugin which provides a way to have colored pseudo-terminal in a vim buffer. You have to run
ConqueTerm(|[V]Split|Tab) sh
and within it run
python path/to/file.py
(no % is possible) though. It can be narrowed down to a mapping:
nnoremap <expr> ,p ":\<C-u>ConqueTermVSplit sh\n\<C-o>:call feedkeys('python '.shellescape(bufname(".bufnr("%").")).\"\\n\")\n"
I have created my own workaround for this which may be valuable to you depending on how you use ipdb. The idea is that you can pass in no_colors=True to set_trace() and that way the interactive debugger will not produce any colour output. I have also enabled this argument for launch_ipdb_on_exception.
This means that you can do:
import ipdb
ipdb.set_trace(no_colors=True)
And the output looks fine in MacVim.
To use this you will have to use my version of ipdb, which is here, actual relevant commit if you want to see what I did is here.
It turns out that ipdb is merely a convient way of accessing ipython.core.debugger, Pdb the actual debugger is defined there.
For windows users i suggest ConEmu. Works perfectly with ipdb (highlighting, auto-complete, ...)
Related
I just made the transition from Spyder to VScode for my python endeavours. Is there a way to run individual lines of code? That's how I used to do my on-the-spot debugging, but I can't find an option for it in VScode and really don't want to keep setting and removing breakpoints.
Thanks.
If you highlight some code, you can right-click or run the command, Run Selection/Line in Python Terminal.
We are also planning on implementing Ctrl-Enter to do the same thing and looking at Ctr-Enter executing the current line.
You can:
open a terminal at Terminal>New Terminal
Highlight the code you want to run
Hit Terminal>Run Selected Text
As for R you can hit CTRL Enter to execute the highlighted code. For python there's apparently no default shortcut (see below), but I am quite sure you can add yours.
In my ver of VSCode (1.25), shift+enter will run selection. Note that you will want to have your integrated terminal running python.
One way you can do it is through the Integrated Terminal. Here is the guide to open/use it: https://code.visualstudio.com/docs/editor/integrated-terminal
After that, type python3 or python since it is depending on what version you are using. Then, copy and paste the fraction of code you want to run into the terminal. It now has the same functionality as the console in Spyder. Hope this helps.
I'm still trying to figure out how to make vscode do what I need (interactive python plots), but I can offer a more complete answer to the question at hand than what has been given so far:
1- Evaluate current selection in debug terminal is an option that is not enabled by default, so you may want to bind the 'editor.debug.action.selectionToRepl' action to whatever keyboard shortcut you choose (I'm using F9). As of today, there still appears to be no option to evaluate current line while debugging, only current selection.
2- Evaluate current line or selection in python terminal is enabled by default, but I'm on Windows where this isn't doing what I would expect - it evaluates in a new runtime, which does no good if you're trying to debug an existing runtime. So I can't say much about how useful this option is, or even if it is necessary since anytime you'd want to evaluate line-by-line, you'll be in debug mode anyway and sending to debug console as in 1 above. The Windows issue might have something to do with the settings.json entry
"terminal.integrated.inheritEnv": true,
not having an affect in Windows as of yet, per vscode documentation.
I've been trying to find this information online but I'm not getting the answer.
I've used RStudio and Geany for editing files before. Now I'm trying to use ViM to edit python and R files (I know there's RPy, but nothing to do with my problem).
I would like to know how can I have 3 terminals (could also be vim buffers, or screen windows) with one running ViM and the others running R and Python. When I execute a Python script, the terminal (window or buffer) with python shows the output. The same when I run R scripts.
I would appreciate insight on this as this is something that's keeping me from using ViM regularly. I would also consider a solution with terminator terminal multiplexer or guake terminal. Any information about sending code for scripting from one instance to another is welcome.
Are you looking for a way to have a REPL inside Vim? If so, Vim wasn't really designed with that in mind, though there are some plugins that try. Conque is an example.
Some things I use to have a quicker code/run/test iteration with Python:
IPython's %edit feature, which starts editing a script with $EDITOR and will run the script after you exit.
vim-ipython which can send/execute/recieve code via an IPython interpreter.
tmux which allows you to have multiple shells side by side, but with little interaction between them.
Vim-slime is a general-purpose solution to this I'm pretty happy about, it will send blocks of code to any tmux pane, meaning it works for any language.
https://github.com/jpalardy/vim-slime
Your requirements for online information may not have been spelled out in enough detail, since I seem to find a wealth of information on using ViM as an IDE for both R and Python:
R:
http://www.r-bloggers.com/r-with-vim/
http://www.vim.org/scripts/script.php?script_id=2628
http://www.vim.org/scripts/script.php?script_id=1048
Python:
http://wiki.python.org/moin/Vim
http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/
http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/
Have a look at vim-ipython, a plug-in for Vim.
You need to download the source (linked above), and run the Vim command :source path/to/file/ipy.vim.
Start by running a new IPython session (e.g. using IPython qtconsole or IPython notebook) and then type :IPython into Vim. Your Vim is now connected to the IPython instance you just opened.
You can press F5 to run the whole python script in your Vim, or Ctrl+s to run the current line. Ctrl+s will also run whatever is selected if you're in visual (i.e. 'select') mode.
I am using Eclipse as a Python IDE. Is there anyway for me to Debug my program and break to an interactive prompt. I am interested in exploring the existing data and running/testing commands.
I believe there has to be a way, but I am so used to compiling languages that I have not been able to find where the options are.
Any ideas?
You can easily do that by using PDB (Python Debugger) inside a python shell.
Look at http://docs.python.org/library/pdb.html for more info.
Anyway I believe Eclipse will let you inspect you data when setting a breakpoint.
Are there any ways to debug python scripts not leaving vim in *nix systems (executing the script, setting up breakpoints, showing variables in watch-list, etc)?
Use pdb:
import pdb
def main():
list = [1,2,3]
pdb.set_trace()
list = [2,3,4]
if __name__ == '__main__':
main()
Now run using :!python % and you'll hit your breakpoint and be able to debug interactively like in gdb.
As of Python 3.7, you can use breakpoint() builtin without importing anything.
Built-in breakpoint() calls sys.breakpointhook(). By default, the latter imports pdb and then calls pdb.set_trace()
Inheriting code from Pierre-Antoine's answer, the code would look like this:
def main():
list = [1,2,3]
breakpoint()
list = [2,3,4]
if __name__ == '__main__':
main()
Source: https://docs.python.org/3/whatsnew/3.7.html#pep-553-built-in-breakpoint
Try pyclewn. It allows to use vim as front end for pdb. You can create/delete break points, control flow of debugging process, look at values of your variables. All from vim!
Also try https://pypi.python.org/pypi/pudb - its like pdb but more advanced. Contains code highlighting, stack, showing avaliable values, etc. Not only-vim solution but for me works perfectly.
Three Steps:
Install:
pip install pudb
Paste set_trace in code
from pudb import set_trace; set_trace()
Run your code
As 2020 the Debugger Adapter Protocol is taken care by vimspector.
Supporting Cpp, Python, Java, Js, Go ...
See my other answer
The vimpdb plugin integrates the Python debugger pdb into the VIM editor.
I do recommend it.
Hope it helps.
Vim and pdb-clone is the combination I use. I use Home - pyclewn which provides a replacement for pdb called pdb-clone that is quite faster than vanilla pdb. It integrates well with vim via a plugin, and the thing I appreciate most is that it takes care of breakpoints outside the code, not setting traces within, thus not messing up my line numbers. It does not have a watch window for python yet. You might have a look at vim-debug too, which I could not get to work with my existing highlighting setup.
See the "Debugging" section in this blog post. It shows how to setup F7 to set breakpoints and Shift+F7 to remove breakpoints. It also uses pdb, as mentioned before. With a little modification, you can replace the use of pdb with ipdb (pdb using ipython), which is a lot nicer to use.
It sounds like you want to use VIM as a Python IDE.
A quick Google search found this and this example, with many more.
EDIT: Well, Ok, it seems likely you've searched more than I have.
I hope someone else has some ideas.
From what I know, there is one more option: You could use Eclipse + PyDev for project managing and Vim as an editor for Eclipse. That way You could use the best of both worlds.
Also, I haven't tried it, but You could try this script.
I would like to be able to drop to the python REPL from the debugger -- if this is not possible is there an easier way to evaluate python expressions in the context of the current breakpoint other than manually adding them all as watch expressions?
There is a dedicated Pydev Console available by clicking on the "New console" dropdown in the console view.
See http://pydev.sourceforge.net/console.html
I don't use pydev, but to drop to python's interactive REPL from code:
import code
code.interact(local=locals())
To drop to python's debugger from code:
import pdb
pdb.set_trace()
Finally, to run a interactive REPL after running some code, you can use python's -i switch:
python -i script.py
That will give you a python prompt after the code, even if it throws an exception.
You may be able to hook some of those solutions into pydev, I think.
As Dag Høidahl said, the PyDev Console is actually the best option (at least on Eclipse Indigo), no need to hack around.
Just go to Open Console:
Then select PyDev Console:
If you need to add specific parameters (for example, Jython tends to miss the python.os VM property), you can change them under Window -> Properties -> PyDev -> Interactive Console.