module path?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Sep 28 19:51:48 EDT 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)>



-- 
Steven



More information about the Python-list mailing list