views:

6487

answers:

2

I want to detect whether module has changed. Now, using inotify is simple, you just need to know the directory you want to get notifications from.

How do I retrieve a module's path in python?

+4  A: 

This was trivial.

Each module has a __file__ variable that shows its relative path from where you are right now.

Therefore, getting a directory for the module to notify it is simple as:

os.path.dirname(__file__)
Cheery
Almost but not quite right -- __file__ is *not* "relative to where you're at right now"; when it's relative (which it will be only when there are relative paths in sys.path), it's relative to *where you were when the module was loaded*.
Charles Duffy
+19  A: 
import a_module
print a_module.__file__

Will actually give you the path to the .pyc file that was loaded, at least on Mac OS X. So I guess you can do

import os
path = os.path.dirname(amodule.__file__)

To get the directory to look for changes.

orestis