可以导入名称包含在变量中的模块吗? [英] Possible to import a module whose name is contained in a variable?

查看:54
本文介绍了可以导入名称包含在变量中的模块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想做类似以下的事情,这些都行不通:


modulename =''模块''

导入模块名


错误是没有名为''modulename''的模块。是否有一个

方式来扩展变量?


问候,


史蒂文

解决方案

2005年3月6日21:34:08 -0800,Steven Reddie< sm*@essemer.com.au>写道:



我想做类似以下的事情,这些都行不通:

modulename =''module' '
导入模块名

错误是没有名为''modulename''的模块。是否有一种方法可以扩展变量?




modulename =''module''

module = __import __(modulename )

Hye-Shik


" Steven Reddie" < sm*@essemer.com.au> schrieb im Newsbeitrag

新闻:f9 ************************** @ posting.google.c om ...



我想做类似以下的事情,这些都行不通:

modulename =''module''
import modulename

错误是没有名为''modulename''的模块。有没有一种方法可以扩展变量?

问候,

史蒂文


modulename =''模块''

cmd =''import''+ modulename

exec(cmd)


检查线程:

如何导入子目录中的所有内容?

此新闻组中。


Claudio

PS MODULES = [''module1'',''module2'']

def libinfo():
对于模块中的m:
__import __(''libinfo。''+ m)
m.libinfo()
CFLAGS + = m.CFLAGS




缩进错误?


Claudio Grondi写道:

" Steven Reddie" < sm*@essemer.com.au> schrieb im Newsbeitrag
新闻:f9 ************************** @ posting.google.c om ...


我想做类似以下的事情,这些都行不通:

modulename =''module''
import modulename
<错误是没有名为''modulename''的模块。是否有一种方法可以扩展变量?



modulename =''module''
cmd =''import''+ modulename
exec(cmd)

检查主题:
如何导入子目录中的所有内容?
在此新闻组中。




请注意,它告诉您使用__import__,而不是exec。 =)


STeVe


Hi,

I want to do something like the following, which doesn''t work:

modulename = ''module''
import modulename

The error is that there is no module named ''modulename''. Is there a
way to get that variable expanded?

Regards,

Steven

解决方案

On 6 Mar 2005 21:34:08 -0800, Steven Reddie <sm*@essemer.com.au> wrote:

Hi,

I want to do something like the following, which doesn''t work:

modulename = ''module''
import modulename

The error is that there is no module named ''modulename''. Is there a
way to get that variable expanded?



modulename = ''module''
module = __import__(modulename)
Hye-Shik


"Steven Reddie" <sm*@essemer.com.au> schrieb im Newsbeitrag
news:f9**************************@posting.google.c om...

Hi,

I want to do something like the following, which doesn''t work:

modulename = ''module''
import modulename

The error is that there is no module named ''modulename''. Is there a
way to get that variable expanded?

Regards,

Steven
modulename = ''module''
cmd = ''import ''+modulename
exec(cmd)

Check also the thread:
How do I import everything in a subdir?
in THIS newsgroup.

Claudio
P.S. MODULES = [ ''module1'', ''module2'' ]

def libinfo():
for m in MODULES:
__import__(''libinfo.''+m)
m.libinfo()
CFLAGS+=m.CFLAGS



indentation error?


Claudio Grondi wrote:

"Steven Reddie" <sm*@essemer.com.au> schrieb im Newsbeitrag
news:f9**************************@posting.google.c om...


I want to do something like the following, which doesn''t work:

modulename = ''module''
import modulename

The error is that there is no module named ''modulename''. Is there a
way to get that variable expanded?



modulename = ''module''
cmd = ''import ''+modulename
exec(cmd)

Check also the thread:
How do I import everything in a subdir?
in THIS newsgroup.



And note that it tells you to use __import__, not exec. =)

STeVe


这篇关于可以导入名称包含在变量中的模块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆