函数定义

  1. __import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module
  2. Import a module. Because this function is meant for use by the Python
  3. interpreter and not for general use it is better to use
  4. importlib.import_module() to programmatically import a module.
  5. The globals argument is only used to determine the context;
  6. they are not modified. The locals argument is unused. The fromlist
  7. should be a list of names to emulate ``from name import ...'', or an
  8. empty list to emulate ``import name''.
  9. When importing a module from a package, note that __import__('A.B', ...)
  10. returns package A when fromlist is empty, but its submodule B when
  11. fromlist is not empty. Level is used to determine whether to perform
  12. absolute or relative imports. -1 is the original strategy of attempting
  13. both absolute and relative imports, 0 is absolute, a positive number
  14. is the number of parent directories to search relative to the current module.

name:引用模块名称,如‘sys’, 'libs/module.a'

fromlist:子模块列表,如果name是a.b的格式,则必须把b加入fromlist中

用途

  • 动态导入模块

例子

  1. import os
  2. def test():
  3. datetime = __import__('datetime')
  4. print datetime.datetime.now()
  5. a = __import__("module.module_a", globals={}, locals={}, fromlist=["module_a"], level=-1)
  6. print a.sub(x, y)
  7. test()

调用不同目录的模块

  • 应该先sys.path.append(模块所在路径),然后再用相对路径引用

例:

在下面这个例子中,由于celery调用,运行路径跟当前路径不同,直接引用module.module_a会报错:无法找到该模块,同时如果__import__的第一个参数用绝对路径,会报错:不可使用文件名引用

正确方法就是先在系统路径中加入当前路径,然后用相对路径引用

  1. from celery import Celery
  2. import os
  3. import sys
  4. current_path = os.path.dirname(os.path.realpath(__file__))
  5. sys.path.append(current_path)
  6. app = Celery('tasks', backend='amqp', broker='amqp://test:123456@127.0.0.1/testhost')
  7. @app.task
  8. def add(x, y):
  9. a = __import__("module.module_a", globals={}, locals={}, fromlist=["module_a"], level=-1)
  10. print a.add(x, y)

【python】__import__的更多相关文章

  1. 【Python②】python之首秀

       第一个python程序 再次说明:后面所有代码均为Python 3.3.2版本(运行环境:Windows7)编写. 安装配置好python后,我们先来写第一个python程序.打开IDLE (P ...

  2. 【python】多进程锁multiprocess.Lock

    [python]多进程锁multiprocess.Lock 2013-09-13 13:48 11613人阅读 评论(2) 收藏 举报  分类: Python(38)  同步的方法基本与多线程相同. ...

  3. 【python】SQLAlchemy

    来源:廖雪峰 对比:[python]在python中调用mysql 注意连接数据库方式和数据操作方式! 今天发现了个处理数据库的好东西:SQLAlchemy 一般python处理mysql之类的数据库 ...

  4. 【python】getopt使用

    来源:http://blog.chinaunix.net/uid-21566578-id-438233.html 注意对比:[python]argparse模块 作者:limodou版权所有limod ...

  5. 【Python】如何安装easy_install?

    [Python]如何安装easy_install? http://jingyan.baidu.com/article/b907e627e78fe146e7891c25.html easy_instal ...

  6. 【Python】 零碎知识积累 II

    [Python] 零碎知识积累 II ■ 函数的参数默认值在函数定义时确定并保存在内存中,调用函数时不会在内存中新开辟一块空间然后用参数默认值重新赋值,而是单纯地引用这个参数原来的地址.这就带来了一个 ...

  7. 【Python】-NO.97.Note.2.Python -【Python 基本数据类型】

    1.0.0 Summary Tittle:[Python]-NO.97.Note.2.Python -[Python 基本数据类型] Style:Python Series:Python Since: ...

  8. 【Python】-NO.99.Note.4.Python -【Python3 条件语句 循环语句】

    1.0.0 Summary Tittle:[Python]-NO.99.Note.4.Python -[Python3 条件语句 循环语句] Style:Python Series:Python Si ...

  9. 【Python】-NO.98.Note.3.Python -【Python3 解释器、运算符】

    1.0.0 Summary Tittle:[Python]-NO.98.Note.3.Python -[Python3 解释器] Style:Python Series:Python Since:20 ...

随机推荐

  1. Hadoop(五)搭建Hadoop客户端与Java访问HDFS集群

    阅读目录(Content) 一.Hadoop客户端配置 二.Java访问HDFS集群 2.1.HDFS的Java访问接口 2.2.Java访问HDFS主要编程步骤 2.3.使用FileSystem A ...

  2. 在js中绑定onclick事件为什么不加括号,在html代码中必须要加?

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 字符串格式的方法%s、format和python3.6新特性f-string和类型注解

    一.%s msg = '我叫%s,今年%s,性别%s' %('帅哥',18,'男') print(msg) # 我叫帅哥,今年18,性别男 二.format # 三种方式: # 第一种:按顺序接收参数 ...

  4. C#操作session的类实例(转)

    using System.Web; namespace DotNet.Utilities { public static class SessionHelper2 { /// <summary& ...

  5. git仓库构建小记

    1.新建 .git 文件夹 约定的文件目录下,新建 .git 文件夹 mkdir test.git 2.初始化服务端仓库 git init --bare test.git 此时进入 test.git ...

  6. Vivado寄存器初始值问题

    前言 本复位只针对Vivado中的寄存器复位. 什么时候需要复位?到底要不要复位?怎么复位?复位有什么卵用? 该复位的寄存器需要复位,复位使得寄存器恢复初始值,有的寄存器并不需要复位(数据流路径上). ...

  7. iPhone各种机型尺寸、屏幕分辨率

    px与pt区别 字体大小的设置单位,常用的有2种:px.pt.这两个有什么区别呢? 先搞清基本概念: px就是表示pixel,像素,是屏幕上显示数据的最基本的点: pt就是point,是印刷行业常用单 ...

  8. adb调试

    adb usb调试,adb网络调试是非常实用的工具,通过电脑连接手机达到文件传输.电脑端安装app刷机等功能材料: 材料: 1.电脑端安装号对应手机的驱动程序 2.电脑端下载好adb调试工具 3.手机 ...

  9. 剑指Offer_编程题_19

    题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2, ...

  10. 启动多个logstash脚本

    一台服务器上启动多个logstash脚本 # more logstash_click #!/bin/sh # Init script for logstash # Maintained by Elas ...