Python内置函数(68)——__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内置函数(68)——__import__的更多相关文章
- Python内置函数(48)——__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 内置函数(一),低阶内置函数功能汇总
python 内置函数 68个 今日主要内容: 1.内置函数 一.内置函数 1.内置函数 详细细节内容地址(id):https://mubu.com/edit/odv-2Dkb6j
- 【转】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 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- 那些年,很多人没看懂的Python内置函数
Python之所以特别的简单就是因为有很多的内置函数是在你的程序"运行之前"就已经帮你运行好了,所以,可以用这个的特性简化很多的步骤.这也是让Python语言变得特别的简单的原因之 ...
- lambda 表达式+python内置函数
#函数 def f1(a,b): retrun a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda p ...
随机推荐
- Python 官方文档解读(1):66 个内置函数
Python 解释器 (CPython 3.7)内置有 66 个函数,这些函数在任何时刻都是可用的.此文是为了对这 66 个函数进行简单的梳理,便于以后可能用到它们时能想到. 1. abs(x) 返回 ...
- Virtual box中Ubuntu虚拟机磁盘碎片整理和空间清理方法
虚拟机中,随着不断的使用,增加大文件(例如日志,视频和软件版本),虽然在虚拟机中手动删除了,但是虚拟机占用的空间并不会随之减少,需要手动清理一下. 这里介绍一种Virtual box中Ubuntu碎片 ...
- python tensorflow model
step01_formula # -*- coding: utf-8 -*- """ 단순 선형회귀방정식 : x(1) -> y - y = a*X + b (a ...
- XYZZY spfa 最长路 判环
题意: 有n个点 m条边 每个边有权值 一开始有一百血 每次经过一条路都会加上其权值 判断是否能够到达n 显然 有正环的时候肯定能够到达 最短路好题!!!!!!! 显用folyed判断是否联通 ...
- python基础day3
一.文件管理 文件管理是很多应用程序的基本功能和重要组成部分.Python可以使文件管理极其简单,特别是和其它语言相对比. 1. 读操作 1.1r模式 1.1.1读取其他路径下文件 首先在D盘创 ...
- MySQL基于左右值编码的树形数据库表结构设计
MySQL基于左右值编码的树形数据库表结构设计 在关系型数据库中设计树形的数据结构一直是一个十分考验开发者能力的,最常用的方案有主从表方案和继承关系(parent_id)方案.主从表方案的最大缺点 ...
- Angular 2项目的环境配置和项目搭建
AngularJS2 发布于2016年9月份,它是基于ES6来开发的. AngularJS2 是一款开源JavaScript库,由Google维护,用来协助单一页面应用程序运行.AngularJS2 ...
- pip离线安装依赖包
pip安装离线本地包 导出本地已有的依赖包 pip freeze > requirements.txt 将依赖包下载到本地 # 下载到当前目录,指定pip源 pip download -r re ...
- 201771010126 王燕《面向对象程序设计(Java)》第九周学习总结
实验九 异常.断言与日志 实验时间 2018-10-25 1.实验目的与要求 (1) 掌握java异常处理技术: 异常积极处理方法:使用try子句捕获异常 异常小计处理方法:抛出throw异常类 (2 ...
- java中class文件与jar文件
1. JAR 文件包 JAR 文件就是 Java Archive File,顾名思意,它的应用是与 Java 息息相关的,是 Java 的一种文档格式.JAR 文件非常类似 ZIP 文件——准确的说, ...