Python内置函数(48)——__import__
英文文档:
__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内置函数(48)——__import__的更多相关文章
- Python内置函数(68)——__import__
英文文档: __import__(name, globals=None, locals=None, fromlist=(), level=0) This function is invoked by ...
- python内置函数之__import__()
__import__(name, globals=None, locals=None, fromlist=(), level=) 用来导入模块. >>> __import__('os ...
- Python内置函数(48)——ord
英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
随机推荐
- android使用JSON数据和服务器进行交互
//点击按钮发送反馈信息给服务端,成功则进入优惠券界面 Button upload = (Button) findViewById(R.id.upload); final String finalLa ...
- 与JavaWeb有关的故事(web请求与Java I/O)
作为一名后端屌丝程序员,对算法.并发.性能乐此不疲.但是,随着年龄和阅历的增加,显然叶落而不知秋的心态是不太能混了.尤其是,某T面试官在明知我是后端,且明确表示对HTTP协议不太熟的情况下,强行让我解 ...
- Spark2.1.0官方文档
Spark 概述 Apache Spark是一个快速和通用的集群计算系统.它提供Java,scala,Python.R语言的APIs,以及支持一般执行图形的优化引擎. 它还支持一组丰富的高级工具,包括 ...
- 原生jdbc操作mysql数据库详解
首先给大家说一下使用JDBC链接数据库的步骤 1.加载链接数据库驱动 2.建立数据库链接 3.创建数据库操作对象 4.编写sql语句,执行sql语句 5.获取结果集 6.释放资源 我这边采用的是mav ...
- 使用mescroll来实现移动端页面上拉刷新, 下拉加载更多功能
* mescroll请参考官方文档 1. 使用mescroll实现下拉滑动的效果: (仅仅效果, 有的页面不需要刷新数据, 只要你能下拉就行) 代码如下: var mescroll = new MeS ...
- [Luogu 1395] 会议
题目 Description 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会议,村长希望所有村民到会议地点的距离之和最小,那 ...
- 笔记:Maven 反应堆
在一个多模块的Maven项目中,反应堆(Reactor)是指所有模块组成的一个构建结构,对于单个模块的项目,反应堆就是该模块本身,但对于多模块项目来说,反应堆就包含了各模块之间继承与依赖的关系,从而能 ...
- MYSQL数据库学习九 数据的操作
9.1 插入数据记录 1. 插入完整或部分数据记录: INSERT INTO table_name(field1,field2,field3,...fieldn) VALUES(value1,valu ...
- 【Python】 更棒的Excel操作模块xlwings
[xlwings] 说到Python操作Excel,有好多模块都可以支持这个工作.比如最底层的win32模块不仅可以操作Excel,还可以操作其他一众windows的软件. 其他的比较熟悉的有xlrd ...
- Matlab绘图基础——散点生成三角网(TIN)
%例一:二维三角网TIN模型的生成 X=rand(10,2)*5; dt=DelaunayTri(X(:,1),X(:,2)); %生成三角网 triplot(dt);hold on; ...