英文文档:

__import__(name, globals=None, locals=None, fromlist=(), level=0)

This function is invoked by the import statement. It can be replaced (by importing the builtins module and assigning to builtins.__import__) in order to change semantics of the import statement, but doing so is strongly discouraged as it is usually simpler to use import hooks (see PEP 302) to attain the same goals and does not cause issues with code which assumes the default import implementation is in use. Direct use of __import__() is also discouraged in favor of importlib.import_module().

The function imports the module name, potentially using the given globals and locals to determine how to interpret the name in a package context. The fromlist gives the names of objects or submodules that should be imported from the module given by name. The standard implementation does not use its locals argument at all, and uses its globals only to determine the package context of the import statement.

level specifies whether to use absolute or relative imports. 0 (the default) means only perform absolute imports. Positive values for level indicate the number of parent directories to search relative to the directory of the module calling __import__() (see PEP 328 for the details).

When the name variable is of the form package.module, normally, the top-level package (the name up till the first dot) is returned, not the module named by name. However, when a non-empty fromlist argument is given, the module named by name is returned.

说明:

  1. 函数功能用于动态的导入模块,主要用于反射或者延迟加载模块。

  2. __import__(module)相当于import module

  先定义两个模块mian.py和index.py,两个文件在同一目录下:

#index.py

print ('index')

def sayHello():
print('hello index') def sayHelloZhCn():
print('你好 index')
#mian.py
print ('main') index = __import__('index')
dir(index)
index.sayHello()
index.sayHelloZhCn()

  执行main.py,可以证实动态加载了index.py,__import__返回的模块也是index模块

C:\Users\Admin\Documents\Python3\importtest>python main.py
main
index
hello index
你好 index

  3. __import__(package.module)相当于from package import name,如果fromlist不传入值,则返回package对应的模块,如果fromlist传入值,则返回package.module对应的模块。

  先定义archives包,其中包含user和role两个模块:

#__index__.py
print ('archives.__index__') def sayHello():
print('hello archives')
#user.py
print ('user') def sayHello():
print('hello user')
#role.py
print ('role') def sayHello():
print('hello role')

  结构如下:

  修改mian.py:

#main.py
print ('main') archives = __import__('archives')
archives.sayHello()
archives.user

  执行main.py,可以证实动态加载了archives包,__import__返回的模块也是archives模块

C:\Users\Admin\Documents\Python3\importtest>python main.py
main
archives.__index__
hello archives
Traceback (most recent call last):
File "main.py", line 5, in <module>
archives.user
AttributeError: module 'archives' has no attribute 'user'

  修改mian.py:

#main.py
print ('main') archives = __import__('archives.user')
archives.sayHello()
print(archives.user)

  执行main.py,可以证实动态加载了archives包的user模块,__import__返回的模块也是archives模块

C:\Users\Admin\Documents\Python3\importtest>python main.py
main
archives.__index__
user
hello archives
<module 'archives.user' from 'C:\\Users\\Admin\\Documents\\Python3\\import
test\\archives\\user.py'>

  修改mian.py:

#main.py
print ('main') archives = __import__('archives.user',fromlist = ('user',))
archives.sayHello()
print(archives)

  执行main.py,可以证实动态加载了archives包的user模块,__import__返回的模块是user模块

C:\Users\Admin\Documents\Python3\importtest>python main.py
main
archives.__index__
user
hello user
<module 'archives.user' from 'C:\\Users\\Admin\\Documents\\Python3\\import
test\\archives\\user.py'>

  4. level参数,指定是使用绝对导入还是相对导入。 0(默认值)表示只执行绝对导入。

Python内置函数(68)——__import__的更多相关文章

  1. Python内置函数(48)——__import__

    英文文档: __import__(name, globals=None, locals=None, fromlist=(), level=0) This function is invoked by ...

  2. python内置函数之__import__()

    __import__(name, globals=None, locals=None, fromlist=(), level=) 用来导入模块. >>> __import__('os ...

  3. python 内置函数(一),低阶内置函数功能汇总

    python  内置函数  68个 今日主要内容: 1.内置函数 一.内置函数 1.内置函数 详细细节内容地址(id):https://mubu.com/edit/odv-2Dkb6j

  4. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  5. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  6. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  7. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  8. 那些年,很多人没看懂的Python内置函数

    Python之所以特别的简单就是因为有很多的内置函数是在你的程序"运行之前"就已经帮你运行好了,所以,可以用这个的特性简化很多的步骤.这也是让Python语言变得特别的简单的原因之 ...

  9. lambda 表达式+python内置函数

    #函数 def f1(a,b): retrun  a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda p ...

随机推荐

  1. 微信小程序之支付密码输入demo

    在小程序中实现支付密码的输入,要解决几个问题: 1.小程序要想唤起键盘,必须要借助input控件.通过input控件和其属性focus来唤起和隐藏输入键盘. 2.要让input控件不可见.让光标和输入 ...

  2. python序列化与反序列

    python序列化与反序列 在python中提供了两个模块可进行序列化.分别是pickle和json.他们两者的功能都差不多,dumps和dump都是进行序列化,而loads和load则是反序列化. ...

  3. java.lang.UnsatisfiedLinkError解决方法汇集(转载)

    我的解决方法: 将sigar.jar拷贝到/WEB-INF/lib/下,但这个方法不知道是不是终极解决办法,暂时没问题,其他方法可参考下面. 运行JSP报表程序页面出现java.lang.Unsati ...

  4. JMeter关联的几种方式总结案例

    1.接口响应结果,通常为HTML.JSON格式的数据,对于HTML的响应结果的提取,可以通过正则表达式,也可以通过XPath 来提取. 2.对于JSON格式的数据,可以通过正则表达式.JSON Ext ...

  5. Extjs在树上加右键菜单--2019-04-15

    效果图如下: 使用规则:将监听加到按钮或树上,监听代码如下. 代码如下: listeners : { //节点单击事件 'rowcontextmenu' : function(view, record ...

  6. python_str的应用

    name = "fsafalk" #nam是个变量名  fsafalk是变量  也是字符串 name.startswith('fs')#判断是否是fs开头 name.endswit ...

  7. 图形上下文导论(Introduction to SWT Graphics)zz

    图形上下文导论(Introduction to SWT Graphics) 摘要: org.eclipse.swt.graphics包(package),包含了管理图形资源的类.只要实现了org.ec ...

  8. PBRT笔记(12)——蒙特卡洛积分

    这里还涉及到pdf.方差等概念,推荐去看<全局光照技术:从离线到实时渲染> 积累分布函数 cumulative distribution function (CDF) 蒙特卡洛估算 为了计 ...

  9. [POJ1193][NOI1999]内存分配(链表+模拟)

    题意 时 刻 T 内存占用情况 进程事件 0 1 2 3 4 5 6 7 8 9 进程A申请空间(M=3, P=10)<成功> 1 A 2 A B 进程B申请空间(M=4, P=3)< ...

  10. 编程菜鸟的日记-《软件测试》Ron Patton著-读书笔记

    第一部分 软件测试综述 第一章 软件测试的背景 1.软件测试员的目标:尽可能早地找到软件缺陷,并确保其能得以修复. 2.仅仅测试程序是否按预期方式运行有何问题:程序能完好的跑通并不代表软件不存在缺陷, ...