Getting file path of imported module

Python

Python Problem Overview


How can I get the file path of a module imported in python. I am using Linux (if it matters).

Eg: if I am in my home dir and import a module, it should return back the full path of my home directory.

Python Solutions


Solution 1 - Python

Modules and packages have a __file__ attribute that has its path information. If the module was imported relative to current working directory, you'll probably want to get its absolute path.

import os.path
import my_module

print(os.path.abspath(my_module.__file__))

Solution 2 - Python

I've been using this:

import inspect
import os
class DummyClass: pass
print os.path.dirname(os.path.abspath(inspect.getsourcefile(DummyClass))

(Edit: This is a "where am I" function - it returns the directory containing the current module. I'm not quite sure if that's what you want).

Solution 3 - Python

This will give you the directory the module is in:

import foo
os.path.dirname(foo.__file__)

Solution 4 - Python

I could be late here, some of the modules would throw AttributeError when using __file__ attribute to find the path. In those case one can use __path__ to get the path of the module.

>>> import some_module
>>> somemodule.__path__
['/usr/lib64/python2.7/site-packages/somemodule']

Solution 5 - Python

To find the load path of modules already loaded:

>>> import sys
>>> sys.modules['os']
<module 'os' from 'c:\Python26\lib\os.pyc'>

Solution 6 - Python

I have been using this method, which applies to both non-built-in and built-in modules:

def showModulePath(module):
        if (hasattr(module, '__name__') is False):
            print 'Error: ' + str(module) + ' is not a module object.'
            return None
        moduleName = module.__name__
        modulePath = None
        if imp.is_builtin(moduleName):
            modulePath = sys.modules[moduleName];
        else:
            modulePath = inspect.getsourcefile(module)
            modulePath = '<module \'' + moduleName + '\' from \'' + modulePath + 'c\'>'
        print modulePath 
        return modulePath

Example:

Utils.showModulePath(os)
Utils.showModulePath(cPickle)

Result:

<module 'os' from 'C:\SciSoft\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\lib\os.pyc'>
<module 'cPickle' (built-in)>

Solution 7 - Python

Try:

> help('xxx')

For example

>>> help(semanage)
Help on module semanage:

NAME
    semanage

FILE
    /usr/lib64/python2.7/site-packages/semanage.py

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncnuView Question on Stackoverflow
Solution 1 - PythonCristianView Answer on Stackoverflow
Solution 2 - Pythonuser9876View Answer on Stackoverflow
Solution 3 - PythondF.View Answer on Stackoverflow
Solution 4 - PythonSanjuView Answer on Stackoverflow
Solution 5 - PythonkjfletchView Answer on Stackoverflow
Solution 6 - PythonTomView Answer on Stackoverflow
Solution 7 - PythonfiroView Answer on Stackoverflow