|
|
@ -616,17 +616,29 @@ def get_root_path(import_name): |
|
|
|
|
|
|
|
|
|
|
|
Not to be confused with the package path returned by :func:`find_package`. |
|
|
|
Not to be confused with the package path returned by :func:`find_package`. |
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
# Module already imported and has a file attribute. Use that first. |
|
|
|
|
|
|
|
mod = sys.modules.get(import_name) |
|
|
|
|
|
|
|
if mod is not None and hasattr(mod, '__file__'): |
|
|
|
|
|
|
|
return os.path.dirname(os.path.abspath(mod.__file__)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Next attempt: check the loader. |
|
|
|
loader = pkgutil.get_loader(import_name) |
|
|
|
loader = pkgutil.get_loader(import_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Loader does not exist or we're referring to an unloaded main module |
|
|
|
|
|
|
|
# or a main module without path (interactive sessions), go with the |
|
|
|
|
|
|
|
# current working directory. |
|
|
|
if loader is None or import_name == '__main__': |
|
|
|
if loader is None or import_name == '__main__': |
|
|
|
# import name is not found, or interactive/main module |
|
|
|
|
|
|
|
return os.getcwd() |
|
|
|
return os.getcwd() |
|
|
|
|
|
|
|
|
|
|
|
# For .egg, zipimporter does not have get_filename until Python 2.7. |
|
|
|
# For .egg, zipimporter does not have get_filename until Python 2.7. |
|
|
|
|
|
|
|
# Some other loaders might exhibit the same behavior. |
|
|
|
if hasattr(loader, 'get_filename'): |
|
|
|
if hasattr(loader, 'get_filename'): |
|
|
|
filepath = loader.get_filename(import_name) |
|
|
|
filepath = loader.get_filename(import_name) |
|
|
|
else: |
|
|
|
else: |
|
|
|
# Fall back to imports. |
|
|
|
# Fall back to imports. |
|
|
|
__import__(import_name) |
|
|
|
__import__(import_name) |
|
|
|
filepath = sys.modules[import_name].__file__ |
|
|
|
filepath = sys.modules[import_name].__file__ |
|
|
|
|
|
|
|
|
|
|
|
# filepath is import_name.py for a module, or __init__.py for a package. |
|
|
|
# filepath is import_name.py for a module, or __init__.py for a package. |
|
|
|
return os.path.dirname(os.path.abspath(filepath)) |
|
|
|
return os.path.dirname(os.path.abspath(filepath)) |
|
|
|
|
|
|
|
|
|
|
|