module path?

Steven D'Aprano steve at
Tue Sep 29 01:51:48 CEST 2009

On Mon, 28 Sep 2009 16:41:36 -0700, akonsu wrote:

> hello,
> is there a way to determine the file location of a loaded module?
> assuming it is not built in.
> import settings
> print settings
> produces: <module 'settings' from 'd:\ko\mysite\settings.pyc'>
> i would like to get to this path somehow other than by parsing the
> string representation of the module. is there a property on
> types.ModuleType? i could not find anything...

Did you look at dir(settings) for a list of method and attribute names?

Look at module.__file__. But be careful because some modules are "built 
in", that is, they don't actually exist as a separate file and are part 
of the Python compiler. E.g.:

>>> import sys
>>> sys.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
>>> sys
<module 'sys' (built-in)>


More information about the Python-list mailing list