How to run python script in emacs

Brian Brian.Mingus at colorado.edu
Mon Sep 28 16:47:48 EDT 2009


I do all my python coding in emacs. It's awesome. Here's how I do it:

*Create windmove bindings*
This is important - it maps *Meta-i,j,k,l* to move your window focus up,
left, down, right. It's used in conjunction with *C-x 3* (split window
vertically) and *C-x 2* (split window horizontally). So this is how you
divide emacs into quadrants and then move around them:
*
C-x 3 C-x 2 M-l M-x 2*

You'll now find yourself in the upper right quadrant. Getting to the lower
left just takes three keystrokes: *M-kj*. Getting back is *M-li*. Highly
efficient. Put this in your .emacs file:

(global-set-key "\M-j" 'windmove-left)
(global-set-key "\M-l" 'windmove-right)
(global-set-key "\M-i" 'windmove-up)
(global-set-key "\M-k" 'windmove-down)

*Create a python buffer*
The easiest way to get a python buffer is to create a python file and then
open it. I use emacs exclusively in no window mode (-nw) since the gui is so
ugly and highlighted code looks so much better on a black background.
*
touch my_python_file.py
emacs -nw my_python_file.py* # you're automatically put in python-mode

You can also just open emacs -nw and create a new buffer like so:

*emacs -nw
C-x b my_python_file.py
C-x C-s my_python_file.py
M-x python-mode*

*Create a python shell that is linked to your buffer*

This is where we really get to show off the power of emacs. After you've got
a single window split emacs horizontally (*C-x 3*). Now you've got two
copies of your my_python_file.py buffer visible, one on the left, one on the
right. We're going to stick a python shell on the right with *C-c !*. Now
your cursor is on the right, in your python shell and your python buffer is
on the left

Let's move back to the python buffer with *M-j* and now let's write a small
bit of code. I want to demonstrate how powerful/useful/efficient this new
mode is by showing you how to execute only* part* of a for loop.

while True:
    print "hi"
    break
    print "bye"

This is obviously some silly code, It's going to print hi once, and then
it's going to stop, and it's never going to print bye. You can test this out
by running py-execute-buffer, which is linked to *C-c C-c*. You should see
the word hi printed in the output of your shell, and your focus is also in
this shell.

Use *M-j* to get back to the python code. Now we're just going to execute
two lines of this code: While True: print "hi" - an infinite loop.

*M-Shift-<*        # takes you back to the beginning of the buffer
*Ctrl-Spacebar* # tells emacs to start highlighting
*Ctrl-n n*           # tells emacs to move the cursor down two lines, and
emacs highlights both of those lines as well.
*Ctrl-c |*            # tells emacs to run the highlighted code only.

Uh-oh, you just ran an infinite loop. 'hi' is being printed in your shell a
zillion times a second. Let's stop it:
*
M-l*         # move over to the shell
*C-c C-c*  # tells emacs to stop the code execution - KeyboardInterrupt

Ok we're good. If you want, you can repeat the above steps, except just
execute the print "bye" part of the for loop:
*
M-j *                  # move back to our python ocde
*M-Shift-<*         # move back to start of buffer
*Ctrl-n n n*         # move to the print "bye" line
*Ctrl-Spacebar * # start highlighting
*Ctrl-e*               # move cursor to the end of the line, highlighting
that line
*Ctrl-c |*              # run this line of code

*The major benefits of this style of coding - efficiency*

After you get a handle on these keyboard shortcuts - and there really aren't
that many - you will be a highly efficient python hacker because of your
enhanced ability to debug. You can open up large python files and execute
the computationally intensive parts of them selectively, and then continue
coding up the file without having to rerun that code. You can hack on
multiple files at the same time in the same shell. You can modify the state
(e.g., variables) of your code execution by going into your shell, and
modifying or deleting it. You can kill your shell (*C-x k enter*) and start
a new one. And if you end up needing a real debugger you can install pydb,
which comes with a handy-dandy emacs mode:
http://bashdb.sourceforge.net/pydb/

On Sat, Sep 26, 2009 at 9:54 AM, devilkin <devilspell at gmail.com> wrote:

> I'm just starting learning python, and coding in emacs. I usually
> split emacs window into two, coding in one, and run script in the
> other, which is not very convenient. anyone can help me with it? is
> there any tricks like emacs short cut?
>
> also please recommand some emacs plug-ins for python programming, i'm
> also beginner in emacs.currently i'm only using python.el. Are any
> plugins supply code folding and autocomplete?
>
> BTW, I'm not a english native speaker, any grammer mistakes, please
> correct them. :)
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090928/6f80b904/attachment-0001.html>


More information about the Python-list mailing list