How to get the location of the module

when i import the module using

import module_name

Can I see where this module is located on my hard drive?

+5
source share
4 answers

It should be noted that packages have an attribute __file__that points to __init__.py, they also have __path__that points to a package directory. So you can use hasattr(module_name, '__path__') and module_name.__path__[0] or module_name.__file__.

Example (in REPL):

import socket, SOAPpy # SOAPpy is a package
socket.__file__
# .../python2.5/socket.pyc
socket.__path__
# AttributeError: 'module' object has no attribute '__path__'
SOAPpy.__file__
# .../2.5/site-packages/SOAPpy/__init__.pyc
SOAPpy.__path__
# ['.../2.5/site-packages/SOAPpy']
+10
source

Try: module_name.__file__.

If you want to "inspect" an object, use: inspect

+3
source
import module_name
module_name.__file__
+1

.

import sys print sys.__file__

0

All Articles