ubelt.util_import module

Expose functions to simplify importing from module names and paths.

Partial Autogenerate:

import netharn as nh closer = nh.export.closer.Closer() from xdoctest import static_analysis as static from xdoctest import utils

closer.add_dynamic(static.split_modpath) closer.add_dynamic(static.modpath_to_modname) closer.add_dynamic(static.modname_to_modpath) closer.add_dynamic(utils.import_module_from_name) closer.add_dynamic(utils.import_module_from_path)

closer.expand([‘xdoctest’]) text = closer.current_sourcecode() print(text)

text = text.replace(‘ from xdoctest.static_analysis import split_modpath, modpath_to_modnamen’, ‘’)

fpath = ub.expandpath(‘~/code/ubelt/ubelt/_autogen_util_import.py’) open(fpath, ‘w’).write(text + ‘n’)

ubelt.util_import.import_module_from_path(modpath, index=-1)[source]

Imports a module via its path

Parameters:modpath (PathLike) – path to the module on disk or within a zipfile.
Returns:the imported module
Return type:module




If the module is part of a package, the package will be imported first. These modules may cause problems when reloading via IPython magic

This can import a module from within a zipfile. To do this modpath should specify the path to the zipfile and the path to the module within that zipfile separated by a colon or pathsep. E.g. /path/to/archive.zip:mymodule.py


It is best to use this with paths that will not conflict with previously existing modules.

If the modpath conflicts with a previously existing module name. And the target module does imports of its own relative to this conflicting path. In this case, the module that was loaded first will win.

For example if you try to import ‘/foo/bar/pkg/mod.py’ from the folder structure:

  • foo/ +- bar/

    +- pkg/
    • __init__.py

    |- mod.py |- helper.py

If there exists another module named pkg already in sys.modules and mod.py does something like from . import helper, Python will assume helper belongs to the pkg module already in sys.modules. This can cause a NameError or worse — a incorrect helper module.


>>> import xdoctest
>>> modpath = xdoctest.__file__
>>> module = import_module_from_path(modpath)
>>> assert module is xdoctest


>>> # Test importing a module from within a zipfile
>>> import zipfile
>>> from xdoctest import utils
>>> from os.path import join, expanduser
>>> dpath = expanduser('~/.cache/xdoctest')
>>> dpath = utils.ensuredir(dpath)
>>> #dpath = utils.TempDir().ensure()
>>> # Write to an external module named bar
>>> external_modpath = join(dpath, 'bar.py')
>>> open(external_modpath, 'w').write('testvar = 1')
>>> internal = 'folder/bar.py'
>>> # Move the external bar module into a zipfile
>>> zippath = join(dpath, 'myzip.zip')
>>> with zipfile.ZipFile(zippath, 'w') as myzip:
>>>     myzip.write(external_modpath, internal)
>>> # Import the bar module from within the zipfile
>>> modpath = zippath + ':' + internal
>>> modpath = zippath + os.path.sep + internal
>>> module = import_module_from_path(modpath)
>>> assert module.__name__ == os.path.normpath('folder/bar')
>>> assert module.testvar == 1
>>> import pytest
>>> with pytest.raises(IOError):
>>>     import_module_from_path('does-not-exist')
>>> with pytest.raises(IOError):
>>>     import_module_from_path('does-not-exist.zip/')

Imports a module from its string name (__name__)

Parameters:modname (str) – module name
Return type:module


>>> # test with modules that wont be imported in normal circumstances
>>> # todo write a test where we gaurentee this
>>> modname_list = [
>>>     'pickletools',
>>>     'lib2to3.fixes.fix_apply',
>>> ]
>>> #assert not any(m in sys.modules for m in modname_list)
>>> modules = [import_module_from_name(modname) for modname in modname_list]
>>> assert [m.__name__ for m in modules] == modname_list
>>> assert all(m in sys.modules for m in modname_list)
ubelt.util_import.modname_to_modpath(modname, hide_init=True, hide_main=False, sys_path=None)[source]

Finds the path to a python module from its name.

Determines the path to a python module without directly import it

Converts the name of a module (__name__) to the path (__file__) where it is located without importing the module. Returns None if the module does not exist.

  • modname (str) – module filepath
  • hide_init (bool) – if False, __init__.py will be returned for packages
  • hide_main (bool) – if False, and hide_init is True, __main__.py will be returned for packages, if it exists.
  • sys_path (list) – if specified overrides sys.path (default None)

modpath - path to the module, or None if it doesn’t exist

Return type:


python -m xdoctest.static_analysis modname_to_modpath:0 pytest /home/joncrall/code/xdoctest/xdoctest/static_analysis.py::modname_to_modpath:0


>>> modname = 'xdoctest.__main__'
>>> modpath = modname_to_modpath(modname, hide_main=False)
>>> assert modpath.endswith('__main__.py')
>>> modname = 'xdoctest'
>>> modpath = modname_to_modpath(modname, hide_init=False)
>>> assert modpath.endswith('__init__.py')
>>> modpath = basename(modname_to_modpath('_ctypes'))
>>> assert 'ctypes' in modpath
ubelt.util_import.modpath_to_modname(modpath, hide_init=True, hide_main=False, check=True, relativeto=None)[source]

Determines importable name from file path

Converts the path to a module (__file__) to the importable python name (__name__) without importing the module.

The filename is converted to a module name, and parent directories are recursively included until a directory without an __init__.py file is encountered.

  • modpath (str) – module filepath
  • hide_init (bool) – removes the __init__ suffix (default True)
  • hide_main (bool) – removes the __main__ suffix (default False)
  • check (bool) – if False, does not raise an error if modpath is a dir and does not contain an __init__ file.
  • relativeto (str, optional) – if specified, all checks are ignored and this is considered the path to the root module.


Return type:



ValueError – if check is True and the path does not exist

xdoctest -m xdoctest.static_analysis modpath_to_modname


>>> from xdoctest import static_analysis
>>> modpath = static_analysis.__file__.replace('.pyc', '.py')
>>> modpath = modpath.replace('.pyc', '.py')
>>> modname = modpath_to_modname(modpath)
>>> assert modname == 'xdoctest.static_analysis'


>>> import xdoctest
>>> assert modpath_to_modname(xdoctest.__file__.replace('.pyc', '.py')) == 'xdoctest'
>>> assert modpath_to_modname(dirname(xdoctest.__file__.replace('.pyc', '.py'))) == 'xdoctest'


>>> modpath = modname_to_modpath('_ctypes')
>>> modname = modpath_to_modname(modpath)
>>> assert modname == '_ctypes'
ubelt.util_import.split_modpath(modpath, check=True)[source]

Splits the modpath into the dir that must be in PYTHONPATH for the module to be imported and the modulepath relative to this directory.

  • modpath (str) – module filepath
  • check (bool) – if False, does not raise an error if modpath is a directory and does not contain an __init__.py file.

(directory, rel_modpath)

Return type:



ValueError – if modpath does not exist or is not a package


>>> from xdoctest import static_analysis
>>> modpath = static_analysis.__file__.replace('.pyc', '.py')
>>> modpath = abspath(modpath)
>>> dpath, rel_modpath = split_modpath(modpath)
>>> recon = join(dpath, rel_modpath)
>>> assert recon == modpath
>>> assert rel_modpath == join('xdoctest', 'static_analysis.py')