Chinaunix首页 | 论坛 | 博客
  • 博客访问: 536062
  • 博文数量: 142
  • 博客积分: 2966
  • 博客等级: 少校
  • 技术积分: 1477
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-07 22:37
文章分类

全部博文(142)

文章存档

2013年(3)

2012年(21)

2011年(53)

2010年(33)

2009年(32)

分类: Python/Ruby

2011-08-09 11:36:33

  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)
阅读(2928) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~