2015-12-12 38 views
1

我仍然與我的URL轉換器的URL,但我有一個問題。 我想導入x。 我說:如何導入列表中的項目?

for x in range(1, 7): 
    import imports[x] 
    time.sleep(0.1) 

我希望它導入列表imports項目x。我該怎麼做?

回答

5

這就是你想要的嗎?

import importlib 

for i in ['a', 'b', 'c']: 
    importlib.import_module('foo.' + i) 

如果你想要的東西更靈活,你可以使用exec

x = 'foo' 
y = 'bar' 
exec('from ' + x + ' import ' + y, locals(), globals()) 
+0

沒有,試圖importlib.import_module(進口[X])。並給出錯誤。 – AlexINF

+1

@ Alex82有什麼錯誤?你正在使用Python 2或Python 3? – Macabeus

+0

哎呀,忘了點安裝! – AlexINF