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 ...
随机推荐
- OpenSSL MD5 API
#include <stdlib.h> #define _GNU_SOURCE /* for getline API */ #include <stdio.h> /* Open ...
- log4j警告:WARN Please initialize the log4j system properly 的解决方法
出现这个问题的原因则是因为没有为log4j建立配置文件导致的.所以解决问题的方法很简单,只要在 src文件目录下建立配置文件即可: 右键点击src -> New -> File 文件 ...
- 我的Python笔记03
摘要: 声明:本文整理借鉴金角大王的Python之路,Day3 - Python基础3,仅供本人学习使用!!! 本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4 ...
- 2.DNN-神经网络推导
DNN就是我们常常说的深度神经网络,可以说由其衍生出来的各种深度算法都在AI界大行其道,今天就好好理一下这个算法.参考的是刘建平老师的博客:http://www.cnblogs.com/pinard/ ...
- 关于<Servlet>定义
1,百度百科定义: Servlet,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容 ...
- 手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于Tensorflow,Python)
通过: 手写数字识别 ----卷积神经网络模型官方案例详解(基于Tensorflow,Python) 手写数字识别 ----Softmax回归模型官方案例详解(基于Tensorflow,Pytho ...
- 操作系统PV编程题目总结一
1.今有一个文件F供进程共享,现把这些进程分为A.B两组,规定同组的进程可以同时读文件F:但当有A组(或B组)的进程在读文件F时就不允许B组(或A组)的进程读文件F.试用P.V操作(记录型信号量)来进 ...
- java中的反射整理
1,什么是反射 反射机制是java语言提供的一种基础功能,它能够赋予成语在运行时进行自省的能力.通过反射我们可以直接操作类或者对象,例如:可以通过反射去获取某个对象的类的定义,属性,方法,还可以修改类 ...
- 2018 Multi-University Training Contest 3 - HDU Contest
题解: solution Code: A. Ascending Rating #include<cstdio> const int N=10000010; int T,n,m,k,P,Q, ...
- JQuery模拟常见的拖拽验证
css部分 <style> #drag{ position: relative; background-color: #e8e8e8; width: 300px; height: 34px ...