Finding a module’s path

If you’re working on a script that has imported a bunch of modules, and you need to know where they actually live so you can take a look at them then use the __file__ command.
Pop open a Python interpreter and do this:

import bar
print bar.__file__

This should print out the modules location path.

Or even better, you can use the inspect module.

In an interpreter or script:

import os
import inspect
inspect.getfile(os)
inspect.getfile(inspect)

This is of course only helpful if you have access to the file system where the modules live. I deal with other people’s code often though, and this helps me find what I need quickly.

Leave a Reply

Your email address will not be published. Required fields are marked *