Finding absolute path of imported module?

Christian Heimes lists at cheimes.de
Tue Jun 19 15:32:53 EDT 2012


Am 19.06.2012 19:55, schrieb Roy Smith:
> So, the question is, is there any way to dump all the *absolute*
> pathnames of all the imported modules?  I can iterate over
> sys.modules.values(), but that doesn't give me absolute pathnames, so
> I can't tell which version of the symlink existed when the module was
> imported.

You can use os.path.abspath(module.__file__) to get the absolute path of
a module. This works reliable unless you use os.chdir() in your code.

abspath() may not normalize symlinks (not sure about it) but you can
check for symlink with os.path.islink() (uses os.lstat) and resolve the
link with os.readlink().

Christian




More information about the Python-list mailing list