Several methods for retrieving a module's path in python

With importing the model, the methods mainly depends on the os.path model:

# method 1
import
_module_ print _module_.__file__

# method 2
import os.path

print os.path.abspath(_module_.__file__)
print os.path.dirname(_model_.__file__)

# method 3
import sys

sys.modules[
'_model_']

 

Without importing the model:

import imp

imp.find_module('_model_')
 

import pkgutil

pkg = pkgutil.get_loader('_model_')
print pkg.filename

 

posted @ 2013-04-02 10:57  n0p  阅读(200)  评论(0编辑  收藏  举报