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']
source
share