ubelt.util_import module¶
-
ubelt.util_import.
split_modpath
(modpath)[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.
Parameters: modpath (str) – module filepath Returns: (directory, rel_modpath) Return type: tuple Example
>>> from xdoctest import static_analysis >>> from os.path import join >>> modpath = static_analysis.__file__ >>> modpath = modpath.replace('.pyc', '.py') >>> dpath, rel_modpath = split_modpath(modpath) >>> assert join(dpath, rel_modpath) == modpath >>> assert rel_modpath == join('xdoctest', 'static_analysis.py')
-
ubelt.util_import.
modpath_to_modname
(modpath, hide_init=True, hide_main=False)[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.
Parameters: - modpath (str) – module filepath
- hide_init (bool) – removes the __init__ suffix (default True)
- hide_main (bool) – removes the __main__ suffix (default False)
Returns: modname
Return type: str
Example
>>> import ubelt.util_import >>> modpath = ubelt.util_import.__file__ >>> print(modpath_to_modname(modpath)) ubelt.util_import
-
ubelt.util_import.
modname_to_modpath
(modname, hide_init=True, hide_main=True, 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.
Parameters: - 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)
Returns: modpath - path to the module, or None if it doesn’t exist
Return type: str
- CommandLine:
- python -m ubelt.util_import modname_to_modpath
Example
>>> from ubelt.util_import import * # NOQA >>> import sys >>> modname = 'ubelt.progiter' >>> already_exists = modname in sys.modules >>> modpath = modname_to_modpath(modname) >>> print('modpath = {!r}'.format(modpath)) >>> assert already_exists or modname not in sys.modules
Example
>>> from ubelt.util_import import * # NOQA >>> import sys >>> modname = 'ubelt.__main__' >>> modpath = modname_to_modpath(modname, hide_main=False) >>> print('modpath = {!r}'.format(modpath)) >>> assert modpath.endswith('__main__.py') >>> modname = 'ubelt' >>> modpath = modname_to_modpath(modname, hide_init=False) >>> print('modpath = {!r}'.format(modpath)) >>> assert modpath.endswith('__init__.py') >>> modname = 'ubelt' >>> modpath = modname_to_modpath(modname, hide_init=False, hide_main=False) >>> print('modpath = {!r}'.format(modpath)) >>> assert modpath.endswith('__init__.py')
-
ubelt.util_import.
import_module_from_name
(modname)[source]¶ Imports a module from its string name (__name__)
Parameters: modname (str) – module name Returns: module Return type: module Example
>>> # test with modules that wont be imported in normal circumstances >>> # todo write a test where we gaurentee this >>> modname_list = [ >>> #'test', >>> '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.
import_module_from_path
(modpath)[source]¶ Imports a module via its path
Parameters: modpath (str) – path to the module Returns: the imported module Return type: module References
https://stackoverflow.com/questions/67631/import-module-given-path
Notes
If the module is part of a package, the package will be imported first. These modules may cause problems when reloading via IPython magic
Warning
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:
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.
Todo
handle modules inside of zipfiles
Example
>>> from ubelt import util_import >>> modpath = util_import.__file__ >>> module = import_module_from_path(modpath) >>> assert module is util_import