函数定义

__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module

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

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

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

用途

  • 动态导入模块

例子

import os

def test():
datetime = __import__('datetime')
print datetime.datetime.now()
a = __import__("module.module_a", globals={}, locals={}, fromlist=["module_a"], level=-1)
print a.sub(x, y) test()

调用不同目录的模块

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

例:

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

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

from celery import Celery
import os
import sys current_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(current_path) app = Celery('tasks', backend='amqp', broker='amqp://test:123456@127.0.0.1/testhost') @app.task
def add(x, y):
a = __import__("module.module_a", globals={}, locals={}, fromlist=["module_a"], level=-1)
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. SQL Server没有足够的内存继续执行程序 (mscorlib)的解决办法

    在Microsoft SQL Server Management Studio 中执行较大的sql脚本时,会报没有足够的内存继续执行程序(mscorlib)的错误.如下图所示 解决方法: 使用sqlc ...

  2. Word Representations 词向量

    常用的词向量方法word2vec. 一.Word2vec 1.参考资料: 1.1) 总览 https://zhuanlan.zhihu.com/p/26306795 1.2) 基础篇:  深度学习wo ...

  3. python学习日记(初识面向对象)

    面向过程 VS 面向对象 面向过程 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数通过切割成小块函数来降低系统 ...

  4. FastDFS分布式文件系统客户端安装

    软件安装前提:服务器已配置好LNMP环境安装libfastcommon见FastDFS服务器安装文档(http://www.cnblogs.com/Mrhuangrui/p/8316481.html) ...

  5. [https]公司导入自签名证书实现https监控

    https://www.v2ex.com/t/143012

  6. secureCRT自动断开的解决方法

    转: secureCRT自动断开的解决方法 secureCRT自动断开的解决方法 在secureCRT上登录时,一段时间不用的话会自动断开,必须重新连接,有点麻烦. 有时候服务器端的 /etc/pro ...

  7. C# 操作SQL Server 公共库

    基本够用,存一下,以后找起来方便 public abstract class DB { //数据库连接字符串(config来配置) protected static string connection ...

  8. Prometheus-配置解析

    1.Prometheus整体架构图 2.配置文件 # 全局配置 global: scrape_interval: 15s # 多久 收集 一次数据 evaluation_interval: 30s # ...

  9. CMDB服务器管理系统【s5day90】:创建资产更新服务器硬盘信息

    1.创建硬件资产信息 import json from django.shortcuts import render,HttpResponse from django.views.decorators ...

  10. Maven 学习总结 (一)

    一.何为Maven 1.Maven是优秀的构建工具 maven的用途之一是用于构建,他是一个强大的构建工具,能够帮助我们自动化构建过程,从清理.编译.测试到生成报告,再到打包和部署. 他抽象了一个完整 ...