Finding absolute path of imported module?

Gelonida N gelonida at gmail.com
Tue Jun 19 17:46:48 EDT 2012


On 06/19/2012 09:32 PM, Christian Heimes wrote:
> 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().
>
If I remember well, os.path.realpath(module.__file__) should normalize 
the paths and resolve the symlinks




More information about the Python-list mailing list