ZhangZhihui's Blog  

 Usage of Dot "." Operator in Relative Import

 

In the above code, "..module4" represents that module4 is present in the parent folder to the current location.

 

importlib Module

Apart from the usual, importlib.import_module() and built-in __import__() can be used for importing a module or package or library. The importlib modules provide an API for interacting with the import system of Python. A direct call  to  __import__ performs only the module search and if the module is found, the module is compiled. The drawbacks in using the components of importing system of Python are as follows.

  • __import__() performs only the module search and if the module is found, the compilation process is executed. The name binding operation is not performed while using __import__().
  • Once the module is compiled, the recent changes in the modules, which are done after compiling the module will not be reflected when importing the modules.

__import__() Function

复制代码
>>> help(__import__)
Help on built-in function __import__ in module builtins:

__import__(name, globals=None, locals=None, fromlist=(), level=0)
    Import a module.

    Because this function is meant for use by the Python
    interpreter and not for general use, it is better to use
    importlib.import_module() to programmatically import a module.

    The globals argument is only used to determine the context;
    they are not modified.  The locals argument is unused.  The fromlist
    should be a list of names to emulate ``from name import ...``, or an
    empty list to emulate ``import name``.
    When importing a module from a package, note that __import__('A.B', ...)
    returns package A when fromlist is empty, but its submodule B when
    fromlist is not empty.  The level argument is used to determine whether to
    perform absolute or relative imports: 0 is absolute, while a positive number
    is the number of parent directories to search relative to the current module.
复制代码

 

posted on   ZhangZhihuiAAA  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
 
点击右上角即可分享
微信分享提示