63

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.

cnu
  • 36,135
  • 23
  • 65
  • 63

7 Answers7

80

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__))
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
Cristian
  • 42,563
  • 25
  • 88
  • 99
  • 4
    AttributeError: module 'tensorflow' has no attribute '__file__' – Rocketq Jun 03 '18 at 20:04
  • I think you could add how to obtain the plain folder name with `os.path.dirname(my_module.__file__)` like described here: https://stackoverflow.com/a/5003468/3734059 – Cord Kaldemeyer Jul 18 '18 at 08:39
  • In my case the code in this answer didn't return anything, but this worked instead `os.path.abspath(my_module)`. The reason for that is I was in a weird situation where a directory had been given the same name of a commonly known module, and this caused the import to import the dir content instead (which didn't even contain python source files). – Redoman Mar 02 '23 at 17:50
13

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).

user9876
  • 10,954
  • 6
  • 44
  • 66
8

This will give you the directory the module is in:

import foo
os.path.dirname(foo.__file__)
dF.
  • 74,139
  • 30
  • 130
  • 136
6

To find the load path of modules already loaded:

>>> import sys
>>> sys.modules['os']
<module 'os' from 'c:\Python26\lib\os.pyc'>
kjfletch
  • 5,394
  • 3
  • 32
  • 38
6

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']
Sanju
  • 1,974
  • 1
  • 18
  • 33
3

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)>
Redoman
  • 3,059
  • 3
  • 34
  • 62
Tom
  • 3,168
  • 5
  • 27
  • 36
  • I couldn't make this work, where did you get the `imp` variable you are using in `imp.is_builtin(moduleName):` ? – Redoman Mar 02 '23 at 17:08
  • 1
    "import imp". In addition, "DeprecationWarning: the imp module is deprecated in favour of importlib and slated for removal in Python 3.12; see the module's documentation for alternative uses" – Tom Mar 03 '23 at 10:27
0

Try:

help('xxx')

For example

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

NAME
    semanage

FILE
    /usr/lib64/python2.7/site-packages/semanage.py
firo
  • 1,002
  • 12
  • 20