[Tutor] function and module

Alan Gauld alan.gauld at btinternet.com
Fri Dec 8 09:21:04 CET 2006

"linda.s" <samrobertsmith at gmail.com> wrote 

>I am reading a sample code and want to figure out where a function
> (for instance, abc) is from.
> There are many lines such as
> from XXX import *

This is one of the chief reasons why 'from x import *' 
is bad practice.

> Is there a way not going through all these imported modules to find
> where the abc is from (by the way, the abc function is not in the
> current module)?

There are a couple of possibilities.
1) if you can import the moduile into a >>> propmpt you could 
ask abc where it's from.
>>> print abc.__file__ should work

2) if the import won't work I think the next best technoque 
is probably to use grep, especially if the modules are all 
in a common directory tree.

grep "def abc" */*.py

Or write a short Python script to traverse the tree using os.walk 
and try doing 'from foo import abc' on each file....


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list