how to get module location
when i import module using
import module开发者_StackOverflow中文版_name
is it possible to see where in my hard disk is that module located?
It is worth mentioning that packages have __file__
attribute which points to __init__.py
, they also have __path__
which points to the 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']
Try: module_name.__file__
.
If you want to 'inspect' an object, use: inspect
import module_name
module_name.__file__
You can use module's file attribute to find the location.
import sys
print sys.__file__
精彩评论