Python内置函数——__import__ 的使用方法
作者:十月狐狸 发布时间:2022-07-18 21:02:18
__import__() 函数用于动态加载类和函数 。
如果一个模块经常变化就可以使用 __import__() 来动态载入。
语法
__import__ 语法:
__import__(name[, globals[, locals[, fromlist[, level]]]])
参数说明:
name -- 模块名
英文文档:
__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(默认值)表示只执行绝对导入。
来源:http://www.cnblogs.com/sesshoumaru/p/6130171.html
猜你喜欢
- 本文实例为大家分享了python批量处理图片的具体代码,供大家参考,具体内容如下公司的一个项目要求把所有4096x4096的图片全部转化成2
- zhanglunray 问:我在mzzx_pic这个层设置了左边距,在ie里显示是正常的,但是在ff里显示时margin-left却没有起到
- Anaconda 是一个旗舰版的python安装包, 因为普通的python没有库, 如果需要安装一些重要的库, 要经常一个一个下载,会非常
- tensorflow2.0实现cnn图像识别import tensorflow as tffrom t
- 几个月前,我开始学习个人形象管理,从发型、妆容、服饰到仪表仪态,都开始做全新改造,在塑造个人风格时,最基础的是先了解自己属于哪种风格,然后找
- 在上篇文章给大家介绍了yii2搭建完美后台并实现rbac权限控制实例教程中完美实现了yii2的后台搭建和rbac权限控制,如果你还没有实现,
- 将训练好的模型参数保存起来,以便以后进行验证或测试,这是我们经常要做的事情。tf里面提供模型保存的是tf.train.Saver()模块。模
- 呵呵,这几天沉溺于灌水,发现转贴的时候真的是很不方便,文字、图形、颜色、连接,如果都转过来真的是满费劲的,于是就写了一个小东西,简陋的很,不
- 前言复习试题时,发现一道复数问题问题关于 Python 的复数类型,以下选项中描述错误的是A复数的虚数部分通过后缀“J”或者“j”来表示B对
- Pandas使用这些函数处理缺失值:isnull和notnull:检测是否是空值,可用于df和seriesdropna:丢弃、删除缺失值ax
- 本文实例讲述了Go语言转换所有字符串为大写或者小写的方法。分享给大家供大家参考。具体如下:Go语言的string模块包含了ToLower和T
- 总结常用基本点如下: 1、触发器有两种类型:数据定义语言触发器(DDL触发器)和数据操纵语言触发器(DML触发器)。 DDL触发器:在用户对
- 一、线性回归的理论1)线性回归的基本概念线性回归是一种有监督的学习算法,它介绍的自变量的和因变量的之间的线性的相关关系,分为一元线性回归和多
- 本文实例讲述了Python简单网络编程。分享给大家供大家参考,具体如下:内容目录1. 客户端(client.py)2. 服务端(server
- 环境: 开发的IDE:JBuilderX 使用的数据库:MS Sql Server 2000 使用的数据库驱动:JSQL Driver(JD
- 阅读上一篇:AJAX的jQuery实现入门(一)要写入数据库,我们知道的最简单的就是注册了, 就做个最简单的注册表单, 看看是如何提交数据的
- 1、XML 是什么?XML仅仅是一种数据存放格式,这种格式是一种文本(虽然XML规范中也提供了存放二进制数据的解决方案)。事实上有很多文本格
- 对于比较长的数字组成的字符串,我们一般会用逗号(,)隔开来格式化数字,从右往左每三个数字用一个逗号分组隔开。为什么要用逗号隔开数字呢?因为当
- 在缺失值填补上如果用前后的均值填补中间的均值,比如,0,空,1,我们希望中间填充0.5;或者0,空,空,1,我们希望中间填充0.33,0.6
- 如果PyPi上搜html2text的话,找到的是另外一个库:Alir3z4/html2text。这个库是从aaronsw/html2text