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

 

你可能感兴趣的:(python)