How to fix pdb in Aquamacs on Mac OS X? - python

I'm developing a django app using aquamacs as my ide. Pdb isn't working since upgrading to emacs 23.2.1 using python 2.6.1. When I invoke pdb like this:
M-x pdb
Run pdb (like this): pdb ./manage.py runserver
The gud-manage.py frame appears with this message (and nothing more) -
Current directory is /path/to/my/source/
It isn't responsive to keyboard input, though I can right-click and send a quit or kill signal. It seems like emacs isn't capturing the pdb output correctly.
Has anyone seen this and (hopefully) fixed it? I believe it has something to do with the gud-pdb-marker-regexp variable (see point #2 in link).
Related issues
Seems to have been around since 2007
One person a solution for this problem on Windows (adding -u to the python command in the pdb script). I tried it anyway, but this didn't work for me.

Same issue (Current directory is ...) to me with emacs 23.2 (9). As you mentioned, it is caused by a CR/LF ending and can be fixed by setting the gud-pdb-marker-regexp.
I added the CR (\r) to the gud-pdb-marker-regexp. May you want to add the following line to your .emacs file and give it a try.
(setq gud-pdb-marker-regexp "^> \\([-axx-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\|<module>\\)()\\(->[^\n\r]*\\)?[\n\r]")

Not sure if that is the case for you, but just to mention it: PDB hangs for me in Emacs when the source code path contains a space - when I move the python file to a directory without a space in the name, it works (on Emacs 23.1.1).

I have been having this same issue. I had it fixed in 23.1 (http://debbugs.gnu.org/db/56/5653.html) but now in 23.2 that fix no longer works, or at least it doesn't appear to for me. I've just submitted a bug to Emacs explaining the problem in detail and hopefully it will get resolved.
A workaround for this is to execute pdb from the emacs shell:
Open the shell: M-x shell
Enter this in the shell: pdb
This will get pdb working properly within the shell.

Related

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.

Python script gives `: No such file or directory`

I have several python scripts which work just fine but one script has (as of this morning) started giving me this error if I try to run it from the bash:
: No such file or directory
I am able to run the 'broken' script by doing python script_name.py and after looking around a bit the general idea that I picked up was that maybe my line ending of the hashbang got changed (silently) so I looked at the line ending of a working script and a broken script via the :set list option in VI as indicated in this question -> View line-endings in a text file
Both files appear to end using the same character (a $) so I am kind of at a loss on how to proceed from here. Specifically, how to actually 'see' the line ending in case the set list was not the right method.
PS: The script is executable and the shebang is in there, I stated that it's just this 1 script that was working fine before the weekend but it started giving me this error as of this morning.
-- edit: --
Running the script through dos2unix does get it working again but I would like to know of any way to visualize the line ending somehow in VI(M) or why Geany somehow converted the line endings in the first place (as I never work on a dos/windows system anyhow).
From the comments above it looks like you have dos line endings, and so the hashbang line is not properly processed.
Line ending style are not shown with :set list in Vim because that option is only used when reading/writing the file. In memory line endings are always that, line-endings. The line ending style used for a file is kept in a Vim per-file option, weirdly called fileformat.
To see/change the line ending style from Vim, you can use the following commands:
:set fileformat
:set ff
It will show dos or unix. You want unix, of course ;-).
To change it quickly you can save the file with:
:w ++ff=unix
Or if you prefer:
:set ff=unix
And then save the file normally.
So see all the gory details just do :help fileformat, :help file-formats and :help fileformats
You can also use the dos2unix command to convert the file format
dos2unix
This helped me to run the python scripts
This normally happens when we open files in windows do changes and save it.
if you open the file locate the ^M characters at the end of every line
Thanks
Personally, I find it kinda wrong using direct path to python interpreter. As you dont use windows platform, you should have program env, usually in /usr/bin (/usr/bin/env). Try using following shebang:
#!/usr/bin/env python
Different distros store python binary in /bin or /usr/bin (or some weird locations), and this one makes your script config-independent (as far as possible, here we have possibility that env is stored elsewhere; still - it is less possible that env is not in /usr/bin than that python is mislocated).
I had similiar problem (if not exactly the same) and that worked for me.
Also, I have both python interpreters (2.7.x and 3.x) installed, so I need to use "python3" argument for env. AFAIR usually distros link different names to different binaries, so "env python" will run python2.7 on my system, "env python3" (also python33, or smth like that) will run p3k, and "env python2" (also python27, etc) will run python 2.7.x. Declaring which version of interpreter should be used seems like a good idea too.
I came across this problem editing my code on Windows, checking it in with git, and checking out and running it on Linux.
My solution was: tell git to Do The Right Thing. I issued this command on the Windows box:
git config --global core.autocrlf true
Modified the files and checked them in; voila, no such problem any more.
As discussed on the Git documentation.

How do I run a python interpreter in Emacs?

I just downloaded GNU emacs23.4, and I already have python3.2 installed in Windows7.
I have been using Python IDLE to edit python files.
The problem is that I can edit python files with Emacs but I do not know how to run python interpreter in Emacs. When i click on "switch to interpreter", then it says "Searching for program: no such file or directory, python"
Someone says i need to make some change on .emacs file, but i do not know where to look for.
And I am very unexperienced and just started to learn programming. I am not familiar with commonly used terminologies. I have been searching for solutions but most of the articles i find on the Internet only confuse me.
so the questions are:
how do i run python interpreter in Emacs?
are there different kind of python interpreter? if so, why do they have different interpreters for one language?
Place this in your .emacs file to set the location of your python interpreter:
(setq python-shell-interpreter "path\to\your\python3.2")
Emacs comes with good manuals and an info mode to help read them. To learn more about .emacs you can use:
M-: (info "(Emacs)Init file") RET.
C-c C-z can do this. It is the key-binding for the command python-switch-to-python
In emacs 25.3.1 I use this to open up a python shell:
M-x run-python
After first adding this to my .emacs file:
(setq python-shell-interpreter "/usr/local/bin/python3")
IF you have python installed, try M-x python-shell
(press and hold ALT while pressing x, then type python-shell, then press enter)
There are different language implementations if that is what you are asking (see a list of them here).
In emacs 24.5.1 with spacemacs 105 (develop branch) and the Python layer enabled ("layer" is a spacemacs concept; see their documentation), I find python-shell-switch-to-shell opens an IPython buffer. I tested macropy.console in such a buffer and it works great.
You probably need to have Python in your windows PATH environment variable. Can you start the interpreter just by typing python in the command window?
I don't have anything special in my emacs.el, but the start interpreter command works just fine.
Also, I recommend reading this blog post, as it contains many useful tips and packages worth installing if using Emacs as a Python IDE.
To simply open an interpreter, you can also use M-x python. If that does not work, try M-x python and hit TAB, which will list more options via auto-completion. One of them should work k if you have python installed.
If you are inclined, it may be worthwhile to check out the Emacs python modes as well - http://www.emacswiki.org/emacs/?action=browse;oldid=PythonMode;id=PythonProgrammingInEmacs
In emacs 24.2 there is python-switch-to-python

IPython workflow (edit, run)

Is there a GUI for IPython that allows me to open/run/edit Python files? My way of working in IDLE is to have two windows open: the shell and a .py file. I edit the .py file, run it, and interact with the results in the shell.
Is it possible to use IPython like this? Or is there an alternative way of working?
When I'm working with python, I usually have two terminal windows open - one with IPython, and the other with a fairly customized Vim.
Two good resources:
http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/
http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/
Though it sounds like what you want is IPython's magic function %ed/%edit:
An example of what you can do:
In [72]: %ed
IPython will make a temporary file named: c:\docume~1\wjwe312\locals~1\temp\ipython_edit_ar8veu.py
In the file I put:
x = "Hello World"
print 3
After saving and quitting the file:
Editing... done. Executing edited code...
3
Out[72]: "x = 'Hello world'\nprint 3\n"
In [73]: x
Out[73]: 'Hello world'
You can define functions or anything else - just remember that the contents of the file will be executed when you close it.
Another similar workflow is to cd to the directory containing your Python script that you're editing with your favorite editor. Then you can %run the script from within IPython and you'll have access to everything defined in the file. For instance, if you have the following in the file test.py in your /home/myself directory:
class Tester(object):
def __init__(self):
print "hi"
def knightme(name):
print "Hello, Sir ", name
Then you can do the following:
In [42]: cd /home/myself
/home/myself
In [43]: %run test.py # <Tab> autocomplete also works
In [44]: knightme('John')
Hello, Sir John
In [45]: t = Tester()
Hi
Either a mix or one of those workflows should give you something very similar to the way you're used to working in IDLE.
Spyder, previously known as SPyderlib / Spyder2
Pretty lightweight, fast and support almost all features you will ever need to work with a python project. It can edit and run .py files in an embedded IPython instance and then interact with them, set breakpoints, etc.
full-size
Try Spyder, I have spent all day trying to find an IDE which has the functionality of ipython and Spyder just kicks it out of the park..
Autocomplete is top notch right from install, no config files and all that crap, and it has an Ipython terminal in the corner for you to instantly run your code.
big thumbs up
Take a look at DreamPie. Might be what you are looking for.
Personally, I like PyScripter. Unfortunately, it only works on Windows, but also runs perfectly in Wine.
The latest version of IdleX supports IPython within IDLE, as well as the %edit magic. You can run your files from the IDLE editor within the IPython shell many ways, either by F5 (run everything), F9 (run a selection), or Ctrl+Enter (run a subcode).
sudo apt-get install ipython
Once you are done with installing ipython.
Start ipython from terminal (just hit ipython in the ternminal)
To run ravi.py file all you need to do is
%run ravi.py
If you like the work-flow under Matlab, then you probably should try the following two:
1, Try the combination of Spyder and Vim.
Edit python files in Vim (Spyder can reload the file automatically)
Run the code in Spyder (in the same interpreter, which is important for me):
Use F9 to run the current file
Ctrl+F9 to run the selected block
2, Use Vim + conque-shell. (on google code)
Open your preferred Python interpreter in Vim,
e.g., just :ConqueTermSplit python.
then visual select some Python code
press F9 to paste and run it in the Python interpreter buffer.
Note: a few more:
:ConqueTermVSplit python,
:ConqueTerm python
:ConqueTermVSplit rlwrap python
If your interpretor misses readline, you can use rlwrap.
You might like PySlices...
It's kind of a shell/editor hybrid that lets you save your session as special (barely) modified python files called .pyslice files.
It's now part of wxPython, so just install that (v2.8.11 or later) and run "python -m wx.py.PySlices" on the command line to launch it.
That said, I still end up using an external editor for scripts (geany).
I want to suggest excellent plugin for vim that makes two-way integration between Vim and IPython: vim-ipython.
From project page on http://github.com/ivanov/vim-ipython:
Using this plugin, you can send lines or whole files for IPython to execute, and also get back object introspection and word completions in Vim, like what you get with: object? and object. in IPython.
This plugin has one big limitation: it doesn't support python 3 (it's planned).
Personally, I use what #Wayne suggested, a combination of vim and ipython...
However, if you'd prefer a different approach, take a look at spyder.
As of the latest version (1.1) ipython should be fully integrated. If you download an earlier version, things will work fine with ipython as an external shell, but you won't get a few of spyder's nifty features (like viewing all of the currently defined variables in the workspace window).
Spyder is definitely a bit heavyweight, but it's an interesting project.
Another (very, very, new) similar project to take a look at is iep. It will (sort-of) work with ipython as shell, and I'd be willing to be bet that nicer ipython integration will be along before too long. At any rate, iep is essentially a more lightweight alternative to spyder.
Both of these are oriented towards scientific computing, and so have nice integration with things like matplotlib (and thus can automatically run gui main loops in a seperate thread). They're not quite like "normal" IDE's but they may fill the niche you're looking for quite nicely.
You can use the autoreload module in IPython to automatically reload code.
Open jupyter qtconsole or jupyter console and type:
%load_ext autoreload
%autoreload 2
from your_work_file import *
Now every time you save your_work_file.py, it will be automatically reloaded.
Hint: if you want this to happen automatically, put the followinglines in your ipython_config.py file:
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
Try Ptpython. It has much better integration with VIM. You can directly edit in VIM by just pressing V. It also allows browsing your history.. so you can pretty much code in the shell, and incrementally build up your code.
If you are already familiar with ipython, you can check the advantages of ptpython here:
https://www.youtube.com/watch?v=XDgIDslyAFM
I just use the exclamation mark (!) to run vi as a shell command
In [1]: !vi myScript.py
and when done with editing I just quit vi to get back to the Ipython shell.
To run the script one can then use
In [2]: %run myScript.py
as suggested in another answer and not !python ... because the Python version in ipython might be different from the one in the underlying shell.
If you want to dump some code in a file use the magic %%writefile
In [3]:%%writefile myScript.py
...: print("hello")
...:
...:
Be careful because this will overwrite myScript.py. To append use %%writefile -a.

How to use Emacs with Python

I am new to emacs and I want to use emacs for python development. I am using Ubuntu 9.10. I frustrated to getting emacs work with python. I use GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.0).
Here what I did.
*
Emacs come with python mode but it is confusing there are two types of mode one is python-mode.el and other one is python.el. I use emacs 23 so mine is python.el (I think). Do I need python-mode too? Code completion does not work when I press M-Tab , instead of it window manager works. I tried Esc-Tab but it says "No match" . How can I enable code completion?
After that I installed ropemacs
sudo aptitude install python-ropemacs
Then I created .emacs file at ~/.emacs
and I added followings to .emacs file
(require 'pymacs)
(pymacs-load "ropemacs" "rope-")
(setq ropemacs-enable-autoimport t)
Then when I hit M-/ (Alt-/) it doesn't work when I cick from the menu Rope->Code assist it opens a file dialog for choosing root project folder. I choose current folder which has there are some python code. When I try again Code assist from menu it says
"Completion for x: " nothing but empty set. How can make emacs python code completion work?
Then I downloaded anything.el, anything-config, anything-match-plugin to ~/.emacs.d folder Then I added following lines to .emacs file
(require 'anything-config)
(require 'anything-match-plugin)
(global-set-key "\C-ca" 'anything)
(global-set-key "\C-ce" 'anything-for-files)
Guess what it didnt work. I tried "M-x anything" again I get No match.(I guessed may me combination of C-ca (First control-a then e ) might work it says it isnt defined). Could you explain code completion for python with clear explanations (step by step) to someone dummy as me. Thanks.
Edit: I able emacs work with python with the link. Thanks all for answering
I haven't tried anything, and I haven't had much luck with rope (giant source tree causes my emacs to hang upon any file save). Instead, I find the default completion works well enough for my purposes.
The default completion keybinding is M-/. That runs dabbrev-expand which expands the current word to "the most recent, preceding word for which this is a prefix." It's not perfect: It won't parse types, and it won't search imports, but it works in 90% of the cases.
(You'll have to deactivate rope.)
I think you do want the package python-mode installed! The ropemacs variants appears to be for refactoring only, and pymacs is allows Python as an Emacs-extension language -- neither of which is what you need for standard support.
I'm not really sure you had to do anything fancy to get Python development to work. On gNewSense deltah (fork of Ubuntu 8.04) all I did was edit a .py file with the first line being:
#!/usr/bin/python
And then Emacs just figures it out and gives you python mode options. I didn't have to install anything beyond Emacs.
Then again, this may not be helpful as gNewSense pre-installs Emacs by default. I'll have to do it on one of my vanilla Ubuntu installs.
Emacs worked out of the box for me on Ubuntu 9.10.
Did you try C-c TAB (update imports) before trying code completion? I don't think it work unless you do that.

Categories

Resources