Take the 2-minute tour ×

In the emacs Python shell (I'm running 2.* Python) I am importing a .py file I'm working with and testing the code. If I change the code however I'm not sure how to import it again.

From my reading so far it seems that


should work, but it doesn't seem to.

Perhaps just shutting down the python shell and restarting it would be enough, is there a command for that or you just do it manually?

edit: It looks like python-send-defun and python-send-buffer would be ideal, but changes don't seem to be propagating.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

It seems to work for me:

Make a file (in your PYTHONPATH) called test.py

def foo():

Then in the emacs python shell (or better yet, the ipython shell), type

>>> import test
>>> test.foo()

Now modify test.py:

def foo():

>>> reload(test)
<module 'test' from '/home/unutbu/pybin/test.py'>
>>> test.foo()
share|improve this answer
Thanks it works perfectly now, I must have been doing something weird. –  justinhj Nov 29 '09 at 20:18
Note that reload will prefer to reload from .pyc files if available. So if you make a change but the pyc files don't get rebuilt or deleted, your change won't appear in the shell. –  rogueleaderr Feb 6 '13 at 19:43
In CPython, reload automatically re-reads the source file if the source file is newer than the .pyc file. I can't find this in the documentation right now, but try it -- it works. –  unutbu Feb 6 '13 at 20:03

While reload() does work, it doesn't change references to classes, functions, and other objects, so it's easy to see an old version. A most consistent solution is to replace reload() with either exec (which means not using import in the first place) or restarting the interpreter entirely.

If you do want to continue to use reload, be very careful about how you reference things from that module, and always use the full name. E.g. import module and use module.name instead of from module import name. And, even being careful, you will still run into problems with old objects, which is one reason reload() isn't in 3.x.

share|improve this answer
the important thing in this comment is that you not only have to re-load the module, but also to re-instantiate objects based on the 'new' module –  Jehiah Nov 29 '09 at 19:13
Just fixing how you import won't make reload work in the general case. If you've stored anything from the imported module (eg. class instances), it won't be reloaded. To make reload work in general, you have to reload all of its users, recursively. Reload is brittle, and I strongly recommend against using it when testing--the last thing anyone needs when debugging is to be bit by a partial reload, leading to mysterious bugs that go away when you restart the shell. –  Glenn Maynard Nov 29 '09 at 21:30

Your Answer


