emacs python pdb hang - python

I have successfully used
M-x pdb
and then type in
python -m pdb myscript.py
and step thru my code before when i have a linux box. Now i have to do all my work in windows. I have downloaded Vincent Goulet's emacs window disbribution and Anaconda2 Python distribution and while all the python shell is working the same M-x pdb step thru my code is not working.
Precisely what happen is that i put in
M-x pdb
Then ask Run pdb (like this):
I try all 3
1) python -m pdb my_script.py
2) python my_script.py
3) my_script.py
i get a new buffer *gud-my_script.py*
in it is
Current directory is c:/User/my_user_name/Documents/python
then it just hang there.
I also try other pdb use case (i.e in script my_script2.py, i wrote)
import pdb; print "hello world1"; pdb.set_trace(); print "line 2"
then just in emacs window command promot run
python my_script2.py
the command promot will not even shows "hello world1" and just freeze there and command prompt won't return
the strange thing is that if i run this NOT in emacs and just in windows command CMD then it will work. i.e. it will print "hello world1" and then drop into debugger.
i did not making any special entry in .emacs after i downloaded Anaconda so to be honestly i don't even know which python mode i am in and why emacs is preventing interaction to show up from pdb.set_trace()
there is similar problem in stack exchange. except i am not doing google app development. Mine is a minimum example.
emacs pdb just hang

Related

Backspace doesn't work in python, in VIM's console with bash (on macOS)

I'm new on StackOverflow, and on VIM, but I really tried to find an answer to my question but found nothing...
I'm on macOS Sierra (latest OS X)
I use MacVim, but the problem is the same if i try on VIM
My backspace works perfectly, every time I need it, in VIM, in bash or in python. Except in that case :
I'm on vim, I try to launch in VIM's console (with :!python) a python program, but when I have to type something, I can't use BackSpace... it writes a ^H (and something as weird as this happens with arrows).
It does the same thing with ipython.
I don't understand how I can make this backspace work specifically in that context ?
In mVIM opened with Conque Term plugin : (But it's the same with :!...)
bash-3.2$ ipython Documents/example1.py
Enter a : 2^H^[[A^[[B^[[C^[[D
Do note that this doesn't happen when :
I launch python or ipython directly in bash (not in VIM)
I launch a program with python or ipython in bash (not in VIM)
I launch python (not a program, only python) or ipython in VIM's bash

python debugging point equivalent to Perl $DB::single=1

I am a Perl programmer learning Python. I am writing my code in emacs debugging with python -m pdb script.py using Python 2.7.3.
I would like to know what is the python equivalent to in Perl adding a $DB::single=1;1; to a specific line of python code, so that when running the debugger, it will stop there, even if it's a different source code file from where the execution started (e.g. a line of code in a library being used by script.py).
Any ideas?
EDITED: after looking at pdb.set_trace() or ipdb.set_trace(), I consider them good solutions but not 100% identical to the behaviour of $DB::single=1;1;. This is, I would like the breakpoint to be on the set_trace line, instead of the next line. This is accomplished in Perl's $DB::single=1; by adding another statement in the same line: 1;, which makes it $DB::single=1;1;.
Using set_trace(), I get the breakpoint at the line after the statement, even if I add 1; after it. Still not fully understanding how Python treats multi-statement lines in comparison to Perl.
Anybody?
Any ideas?
Is the following satisfying your needs ?
import ipdb; ipdb.set_trace()
just write it somewhere in your code and run your script with python script.py.
you need the ipython debugger (ipython is an enhanced python interpreter):
pip install ipdb
edit: did you know that if you run M-x pdb RET pdb myscript.py RET, you'll have a pdb prompt and emacs will track the source code in another buffer, but it doesn't stop where you defined ipdb.set_trace() ?
Virtual Env ?
if you use virtual envs, you have a couple of options. I recommand installing virtualenvwrapper from ELPA and run M-x venv-workon.
Python comes with debugger called pdb. To stop a script at given point in code put the following
import pdb; pdb.set_trace()
Since you are using emacs, you would may want to try out the command pdb provided by gud.el (correction: You do not need to preload 'python-mode' to run pdb, thanks #Andreas Röhler for correction) . Start it by pdb name_of_script.py, then you can set breakpoint from emacs by pressing C-xSPACE at the line you want to set breakpoint at. I recommend you to use menu-bar to explore the commands provided by the debugger (GUD). You can also use the usual pdb commands in the *gud-pdb* buffer started by emacs.

Getting control of `pdb` back from App Engine in Emacs

I have a Python Google App Engine application I'd like to debug on the dev server, in Emacs. I have a pdb executable file I created so that debugging would play nice with Emacs:
$ which pdb
/usr/bin/pdb
$ cat /usr/bin/pdb
#/bin/sh
exec python -m pdb "$#"
In Emacs, I M-x pdb and get prompted Run pdb (like this): to which I enter pdb /usr/local/bin/dev_appserver.py /Users/[person]/path/to/app/directory.
This starts off nicely. I get a window with a (Pdb) prompt, I can set breakpoints successfully in early parts of the code like some of the dev_appserver.py file and use commands like n to step through line at a time. I can then enter c to continue with program execution.
Problem is, as soon as app engine has printed out its usual startup INFO messages (to the same buffer the Pdb session is taking place in), I don't get a (Pdb) prompt back again, so I can't enter any more pdb commands. This is both my first time using pdb and my first time debugging in Emacs, so maybe I'm just doing something plain wrong.
#TimHoffman has a good answer. The dev server re-routes pdb from the various actual server processes, so you're not going to be able to launch pdb from the command line.
An alternative which might work is to launch the dev server from your emacs command line without pdb, and insert a pdb breakpoint in your code
import pdb
pdb.set_trace()
I typically debug with this, but not via emacs.

Python mode in Emacs: No such file or directory, pdb

I have a python script that I want to debug with python-mode. I read in this thread that I can debug my python script with M-x pdb, however I get the following error:
Searching for program: no such file or directory, pdb
I can provide python -m pdb my_source_file.py in the prompt in the minibuffer, but it would be nice if Emacs could infer this command directly from the file on which I run M-x pdb
Update:
Running on:
Red Hat Enterprise Linux Server release 5.1 (Tikanga)
Emacs 23.3.1
Differences between paths
I get different paths when I run M-: exec-path and when I run M-: (getenv "PATH") (the one returned by M-: (getenv "PATH") is longer).
With this:
Where is pdb located? How can I add it to the Emacs path?
Is there a way to ask Emacs to also look into the paths held by the environment variable PATH?
Further to my comment earlier, and your subsequent update to the question:
First figure out a value for $PATH that works in your terminal. Use which pdb to find where the pdb executable is located.
Then, set the $PATH environment variable explicitly in Emacs, and sync it to exec-path as follows:
(setenv "PATH" "/usr/local/bin:/usr/bin:/bin:/some/other/dir")
(setq exec-path (split-string (getenv "PATH") path-separator))
It's possible you would need to also explicitly set PYTHONPATH or similar environment variables; you can do that using lines like the "setenv" line above, or just use the exec-path-from-shell elisp package.
Update
Okay, so it turns out Emacs' pdb command isn't provided by python-mode, and it expects to find an executable called "pdb". The easy way to fix this, then is to create a shell wrapper called "pdb", in a directory on your $PATH:
#!/bin/sh
exec python -m pdb "$#"
(I found a note here suggesting this technique.)
The equivalent under Windows would be a file called pdb.bat, containing:
python -u -m pdb %1
(The -u prevents Python from buffering its output.)
To run the Python Debugger, M-x pdb expects to find an executable named pdb. While the pdb executable may exist in some Python distributions, it doesn't exist in all of them.
A proposal to fix this is in GNU bug report #21521: pdb default suggested command.
Until the bug is fixed, you can set the variable gud-pdb-command-name to define the command used to launch pdb. In .emacs, add...
(setq gud-pdb-command-name "python -m pdb")
At a shell prompt type
which pdb
In Emacs, type M-x customize. Select Programming > Tools > Gud. Set the value of gud-pdb-command-name to the path returned by which pdb.
If your version of Emacs presents a different organization for the customize menu, you could also try
C-h v gud-pdb-command-name
Then click on the customize link, and set the path to pdb there.
Though the instructions above are different, I found this out by reading "Running pdb under emacs" .
You can create a custom command like this:
;; PDB command line
(defun user-python-debug-buffer ()
"Run python debugger on current buffer."
(interactive)
(setq command (format "python -u -m pdb %s " (file-name-nondirectory buffer-file-name)))
(let ((command-with-args (read-string "Debug command: " command nil nil nil)))
(pdb command-with-args)))
In Emacs 23.3.1 and presumably higher, yet another variation is to use the Emacs shell, Eshell (M-x eshell). Under Eshell, there's a pre-existing, Lisp-based definition of pdb. These Lisp functions work in Eshell just like ordinary shell commands.
So pdb "./manage.py runserver" will start a Django server, for instance.
Everyone is going wild saying you gotta make a pdb file and make it an executable and then type ./pdb your_code.py. It is easier than that.
Be where you want to run the debugger from. Probably in your python file, maybe use M-x cd to get somewhere.
Then type: M-x pdb
It will prompt you with:
Run pdb (like this):
You want to make that look like:
Run pdb (like this): python -m pdb your_code.py
Sometimes if I want to run my code as a module.
Run pdb (like this): python -m pdb -m some_package.my_code
Then type help and go read this https://docs.python.org/3/library/pdb.html
My answer builds on what #Chad Nouis mentioned.
a link
However, I've added this to python-mode, everytime python-mode loads, it will set gud-pdb-command-name to "python -m pdb"
;; Set the PDB command
(add-hook 'python-mode-hook
(lambda () (setq gud-pdb-command-name "python -m pdb")))

Debugging python programs in emacs

How do I debug python programs in emacs? I'm using python-mode.el
I've found references suggesting:
import pdb; pdb.set_trace();
but I'm not sure how to use it.
Type M-x cd to change directory to the location of the program you wish to debug.
Type M-x pdb. You'll be prompted with Run pdb (like this): pdb. Enter the name of the program (e.g. test.py).
At the (Pdb) prompt, type help to learn about how to use pdb.
Alternatively, you can put
import pdb
pdb.set_trace()
right inside your program (e.g. test.py). Now type M-x shell to get a shell prompt. When you run your program, you'll be dumped into pdb at the point where pdb.set_trace() is executed.
For me, I needed to replace the default "pdb" with
python -m pdb myscript.py
The realgud package (available from MELPA) supports PDB (among a gazillion other debuggers), and has a host of neat features that Emac's PDB doesn't have.
The one I like best is the shortkeys mode. Once you start debugging a program, you can press n, s, c etc. right in the source window, instead of having to type these commands in the PDB buffer. It also supports Visual-Studio style keybindings with function keys (f10, f11, f5, etc).
After installing RealGUD, you need to run M-x load-feature realgud to load it, and you can start pdb with M-x realgud:pdb.

Categories

Resources