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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律