Python内置函数——__import__ 的使用方法

 更新时间:2017年11月24日 09:19:12   作者:十月狐狸  
本篇文章主要介绍了Python内置函数——__import__ 的使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

__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(默认值)表示只执行绝对导入。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • python如何为创建大量实例节省内存

    python如何为创建大量实例节省内存

    这篇文章主要为大家详细介绍了python如何为创建大量实例节省内存,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Python Pandas pandas.read_sql_query函数实例用法分析

    Python Pandas pandas.read_sql_query函数实例用法分析

    在本篇文章里小编给大家整理的是一篇关于Python Pandas pandas.read_sql_query函数实例用法分析内容,有兴趣的朋友们可以跟着学习下。
    2021-06-06
  • 详解Python进程间通信之命名管道

    详解Python进程间通信之命名管道

    本篇文章主要介绍了详解Python进程间通信之命名管道,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • 使用Python操作Jenkins的过程详解

    使用Python操作Jenkins的过程详解

    借助于Python中的python-jenkins模块,我们可以轻松地编写脚本来连接到Jenkins服务器,并执行各种操作,如创建、删除、构建Jobs等,这种自动化的方式不仅提高了效率,还使得CI/CD流程更加灵活和可控,本文介绍如何使用Python操作Jenkins的相关资料,需要的朋友可以参考下
    2024-05-05
  • python3 如何解压缩.gz文件

    python3 如何解压缩.gz文件

    这篇文章主要介绍了python3 如何解压缩.gz文件,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • Python创建对称矩阵的方法示例【基于numpy模块】

    Python创建对称矩阵的方法示例【基于numpy模块】

    这篇文章主要介绍了Python创建对称矩阵的方法,结合实例形式分析了Python基于numpy模块实现矩阵运算的相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • 使用Python实现网络设备配置备份与恢复

    使用Python实现网络设备配置备份与恢复

    网络设备配置备份与恢复在网络安全管理中起着至关重要的作用,本文为大家介绍了如何通过Python实现网络设备配置备份与恢复,需要的可以参考下
    2025-03-03
  • python修改pip install默认安装路径的详细步骤

    python修改pip install默认安装路径的详细步骤

    pip安装的第三方库默认存放在C盘中,为了便于管理和不过度占用C盘空间所以想修改默认的pip路径,文章通过图文结合的方式给大家介绍的非常详细,需要的朋友可以参考下
    2025-04-04
  • Python自动化实战之接口请求的实现

    Python自动化实战之接口请求的实现

    本文为大家重点介绍如何通过 python 编码来实现我们的接口测试以及通过Pycharm的实际应用编写一个简单接口测试,感兴趣的可以了解一下
    2022-05-05
  • Python使用PIL库实现验证码图片的方法

    Python使用PIL库实现验证码图片的方法

    这篇文章主要介绍了Python使用PIL库实现验证码图片的方法,结合实例形式较为详细的分析了Python基于PIL库生成验证码图片的相关技巧与注意事项,需要的朋友可以参考下
    2016-03-03

最新评论