libb.import_non_local

import_non_local(name, custom_name=None)[source]

Import a module using a custom name to avoid local name conflicts.

Useful when you have a local module with the same name as a standard library or third-party module.

Parameters:
  • name (str) – The original module name.

  • custom_name (str) – Custom name for the imported module.

Returns:

The imported module with the custom name.

Return type:

ModuleType

Raises:

ModuleNotFoundError – If the module cannot be found.

Example:

>>> create_mock_module('mock_calendar')
>>> import mock_calendar
>>> mock_calendar.isleap = lambda year: year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

>>> calendar = import_non_local('calendar', 'mock_calendar')
>>> 'mock_calendar' in sys.modules
True
>>> calendar.isleap(2020)
True