[SciPy-User] How to make sure that a module gets re-loaded

George Nurser gnurser@googlemail....
Sun Oct 25 03:47:22 CDT 2009

2009/10/24 Gaetan Cesar Koers <ckoers@telenet.be>:
> Hello,
> How to make sure that a module gets re-loaded when I've edited it's
> source code?
> I thought that either 'import' or 'reload' could do this, but I have the
> idea that only a cold shell restart helps.
> The time I've lost by chasing a bug that afterwards proves to be a
> remnant of a previous module version!

reload will do this if your module is pure python. If it is written in
C or FORTRAN you need to restart the ipython shell.

> 2nd, related perhaps:
> The code I'm working on, in module 'M.py', is located in a directory
> named 'M'. From the parent of directory 'M', when I do 'import M' or
> 'reload M', how can I make sure that Python imports/reloads 'M/M.py'?

There are at least three ways to do this. Below abspath-M is the full
absolute path to directory M, and relpath-M the relative path

1. Add abspath-M to the PYTHONPATH environment variable
2. In ipython do
   import sys
   sys.path.append('abspath') OR sys.path.append('relpath')

[so e.g. if you are running ipython from the parent of M, simply

3. Create a file named M.pth in a directory already in sys.path. This
file should contain the line
abspath ( or relpath). I put .pth files in my
$HOME/lib/python2.5/site-packages directory.
You can find out where your personal site-packages directory is by
inspecting the path from python or ipython with
print '\n'.join(sys.path)

So e.g. if your modules are in   /users/xxx/bigproject/M
and you have a site-packages directory at
Create a file /users/xxx/lib/python2.5/site-packages/M

with content  (just one line)

or with content

Method 3 is cleanest, but allows a python program running from *any*
directory to access your modules in /users/xxx/bigproject/M, as does
method 1.

HTH. George Nurser.

More information about the SciPy-User mailing list