Coder Perfect

How can I get the path of a module?


I’m trying to figure out if a module has changed. Inotify is very simple to use; all you need to know is which directory you wish to receive notifications from.

In Python, how do I get the path of a module?

Asked by Cheery

Solution #1

import a_module

On Mac OS X, this will really provide you the path to the.pyc file that was loaded. So, I suppose you could:

import os
path = os.path.abspath(a_module.__file__)

Alternatively, you might try:

path = os.path.dirname(a_module.__file__)

To retrieve the directory for the module.

Answered by orestis

Solution #2

Python has an inspect module.


>>> import os
>>> import inspect
>>> inspect.getfile(os)
>>> inspect.getfile(inspect)
>>> os.path.dirname(inspect.getfile(inspect))

Answered by Tomas Tomecek

Solution #3

The best method to do this, as previous responses have shown, is to use __file__ (demonstrated again below). However, there is one essential caveat: if you run the module on its own (i.e. as __main__), __file__ does not exist.

Consider the following two files (both of which are located in your PYTHONPATH):

import bar


import os

The following is the outcome of running

/path1        # "import bar" causes the line "print(os.getcwd())" to run
/path2/ # then "print(__file__)" runs
/path2/ # then the import statement finishes and "print(bar.__file__)" runs

If you try to run on its own, though, you’ll get:

/path2                              # "print(os.getcwd())" still works fine
Traceback (most recent call last):  # but __file__ doesn't exist if is running as main
  File "/path2/", line 3, in <module>
NameError: name '__file__' is not defined 

I hope this information is useful. While testing the other alternatives, this caveat cost me a lot of time and confusion.

Answered by mcstrother

Solution #4

I’ll also try to answer a couple versions of this question:

(Some of these questions were originally posted on SO, but they were marked as duplicates and redirected here.)

If you’ve imported a module, you’ll need to do the following:

import something

will return the module’s absolute path. However, using the script as an example:
print '__file__', __file__

Using the ‘python’ command will just return ‘’. If you throw in a shebang:

print '__file__', __file__

It will return ‘./’ if you call it with./ Calling it from a different directory (e.g., in directory bar), then calling one of the two methods.

python bar/

Alternatively, you can add a shebang and run the file directly:


‘bar/’ will be returned (the relative path).

From there, using os.path.dirname( file ) to find the directory can be hard. If you call it from the same directory as the file, it returns an empty string, at least on my machine. ex.

import os
print '__file__ is:', __file__
print 'os.path.dirname(__file__) is:', os.path.dirname(__file__)

will output:

__file__ is:
os.path.dirname(__file__) is: 

In other words, it returns an empty string, which makes it untrustworthy for usage with the current file (as opposed to the file of an imported module). You can get around this by wrapping it in an abspath call:

import os
print 'os.path.abspath(__file__) is:', os.path.abspath(__file__)
print 'os.path.dirname(os.path.abspath(__file__)) is:', os.path.dirname(os.path.abspath(__file__))

It produces something along the lines of:

os.path.abspath(__file__) is: /home/user/bar/
os.path.dirname(os.path.abspath(__file__)) is: /home/user/bar

It’s worth noting that abspath() doesn’t work with symlinks. If you wish to achieve this, instead use realpath(). Create a symlink named file import testing link that points to file import and has the following content:

import os
print 'abspath(__file__)',os.path.abspath(__file__)
print 'realpath(__file__)',os.path.realpath(__file__)

executing will output absolute paths such as:

abspath(__file__) /home/user/file_test_link
realpath(__file__) /home/user/

file_import_testing_link ->

The inspect module is mentioned by @SummerBreeze.

For imported modules, this appears to function well and is pretty concise:

import os
import inspect
print 'inspect.getfile(os) is:', inspect.getfile(os)

Returns the absolute path obediently. To get the path of the currently running script, use the following formula:


(thanks @jbochi)


gives the absolute path of currently executing script (thanks @Sadman_Sakib).

Answered by jpgeek

Solution #5

I’m not sure why no one has mentioned it, but the simplest approach for me is to use imp.find module(“modulename”) (documentation here):

import imp

It returns a tuple in which the path is in second place:

(<open file '/usr/lib/python2.7/', mode 'U' at 0x7f44528d7540>,
('.py', 'U', 1))

The advantage of this technique over the “inspect” method is that it doesn’t require you to import the module and you may use a string as input. For example, while inspecting modules called from another script.


The importlib module in Python 3 should accomplish the following:

Doc of importlib.util.find_spec:

Answered by PlasmaBinturong

Post is based on