- 论坛徽章:
- 1
|
回复 #7 3227049 的帖子
学习一下
__import__(...)
__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module
Import a module. The globals are only used to determine the context;
they are not modified. The locals are currently 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
试了一下
- def ImportDef(pkgdir,pkgname):
- try:
- module_name = pkgdir + '.' + pkgname
- from_list = [pkgname]
- global m
- m = __import__(name=module_name, fromlist=from_list)
- return True
- except Exception,e:
- print "Error Info: %s" % e
- return False
- m = 0
- ImportDef('os','path')
- help(m)
- dir(m) # no output???
复制代码
奇怪的是为什么最后的dir没有输出呢
在命令行下help和dir都有输出,为什么写入脚本后执行只有help有输出,dir就没有? |
|