2013-12-08 33 views
0

我想'導入...爲'動態。如何使用__import __()函數實現'import ... as'?

我知道,進口在Python的背景,例如:

import foo 

相當於

foo = __import__('foo', globals(), locals(), [], -1) 

調用__import__()如何import foo as bar通話__import__()

+7

它只是將結果賦給'bar'。所以只要做'bar = __import __(無論)'。 – BrenBarn

+0

@BrenBarn - 好吧,那很簡單,不是嗎?不妨將它作爲答案發布,並用它來完成:) – mgilson

回答

7

它只是給結果分配吧。所以只需要做bar = __import__(whatever)

相關問題