分享

python动态加载模块

 java_laq小馆 2014-05-22
python动态加载模块 2011-08-09 11:36:33

分类: Python/Ruby

  1. import imp
  2. import sys

  3. def __import__(name, globals=None, locals=None, fromlist=None):
  4.     # Fast path: see if the module has already been imported.
  5.     try:
  6.         return sys.modules[name]
  7.     except KeyError:
  8.         pass

  9.     # If any of the following calls raises an exception,
  10.     # there's a problem we can't handle -- let the caller handle it.

  11.     fp, pathname, description = imp.find_module(name)

  12.     try:
  13.         return imp.load_module(name, fp, pathname, description)
  14.     finally:
  15.         # Since we may exit via an exception, close fp explicitly.
  16.         if fp:
  17.             fp.close()
 
  1. import imp
  2. m = imp.load_source("m", "/from/some/path/m.py")
  3. print m
  4. print dir(m)

来自:http://blog./uid-20544356-id-2145627.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多