Python论坛  - 讨论区

标题:[python-chinese] 怎样动态import 文件

2007年04月01日 星期日 22:29

Alex Cheng chengxin1985在gmail.com
星期日 四月 1 22:29:51 HKT 2007

ÊÔÊÔ Õâ¸ö:

moduleName = "string"
eval("import " + moduleName)


ÔÚ07-3-31£¬jessinio smith <jessinio在gmail.com> дµÀ£º
>
> __import__(...)
>     __import__(name, globals, locals, fromlist) -> 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.
>
>
> On 3/31/07, PianoPan < pianopan在gmail.com> wrote:
> >
> > Ò²¾ÍÊǸù¾Ý±äÁ¿µÄÄÚÈÝ°üº¬²»Í¬µÄÎļþ£¬ÓÉÔËÐÐʱ±äÁ¿À´¾ö¶¨Òªimport µÄÎļþµÄÃû×Ö
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
> --
> ×¢ÒâÉíÌ壬ÉíÌåÊǸïÃüµÄ±¾Ç®£¡£¡
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
powered by python
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070401/44d0bf3b/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年04月01日 星期日 22:32

Alex Cheng chengxin1985在gmail.com
星期日 四月 1 22:32:06 HKT 2007

²»ºÃÒâ˼, Ū´íÁË.
Ó¦¸ÃÊÇ:
moduleName = "string"
exec("import " + moduleName)

ÔÚ07-4-1£¬Alex Cheng <chengxin1985在gmail.com> дµÀ£º
>
> ÊÔÊÔ Õâ¸ö:
>
> moduleName = "string"
> eval("import " + moduleName)
>
>
> ÔÚ07-3-31£¬jessinio smith < jessinio在gmail.com> дµÀ£º
> >
> > __import__(...)
> >     __import__(name, globals, locals, fromlist) -> 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.
> >
> >
> > On 3/31/07, PianoPan < pianopan在gmail.com> wrote:
> > >
> > > Ò²¾ÍÊǸù¾Ý±äÁ¿µÄÄÚÈÝ°üº¬²»Í¬µÄÎļþ£¬ÓÉÔËÐÐʱ±äÁ¿À´¾ö¶¨Òªimport µÄÎļþµÄÃû×Ö
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> >
> > --
> > ×¢ÒâÉíÌ壬ÉíÌåÊǸïÃüµÄ±¾Ç®£¡£¡
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> powered by python




-- 
powered by python
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070401/39bbd3f4/attachment-0001.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年04月02日 星期一 18:59

Linker Lin linkerlin88在gmail.com
星期一 四月 2 18:59:46 HKT 2007

这个是可以的。

On 4/1/07, Alex Cheng <chengxin1985在gmail.com> wrote:
>
> 不好意思, 弄错了.
> 应该是:
> moduleName = "string"
> exec("import " + moduleName)
>
> 在07-4-1,Alex Cheng < chengxin1985在gmail.com> 写道:
> >
> > 试试 这个:
> >
> > moduleName = "string"
> > eval("import " + moduleName)
> >
> >
> > 在07-3-31,jessinio smith < jessinio在gmail.com> 写道:
> > >
> > > __import__(...)
> > >     __import__(name, globals, locals, fromlist) -> 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.
> > >
> > >
> > > On 3/31/07, PianoPan < pianopan在gmail.com> wrote:
> > > >
> > > > 也就是根据变量的内容包含不同的文件,由运行时变量来决定要import 的文件的名字
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese在lists.python.cn
> > > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request在lists.python.cn
> > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> > >
> > >
> > > --
> > > 注意身体,身体是革命的本钱!!
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request在lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> >
> >
> >
> > --
> > powered by python
>
>
>
>
> --
> powered by python
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
Linker M Lin
linkerlin88在gmail.com
  ※※※※※※※※※
  ※※我思故我在※※
  ※※※※※※※※※
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070402/1a21ed2a/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年04月03日 星期二 11:22

PianoPan pianopan在gmail.com
星期二 四月 3 11:22:43 HKT 2007

哈哈,谢谢,不过我现在用的是这个,用来包含多层目录下的
def libimport(name):
    mod = __import__(name)
    components = name.split('.')
    for comp in components[1:]:
        mod = getattr(mod, comp)
    return mo

On 4/2/07, Linker Lin <linkerlin88在gmail.com> wrote:
> 这个是可以的。
>
>
> On 4/1/07, Alex Cheng <chengxin1985在gmail.com> wrote:
> >
> > 不好意思, 弄错了.
> > 应该是:
> > moduleName = "string"
> > exec("import " + moduleName)
> >
> >
> > 在07-4-1,Alex Cheng < chengxin1985在gmail.com > 写道:
> >
> > > 试试 这个:
> > >
> > > moduleName = "string"
> > > eval("import " + moduleName)
> > >
> > >
> > >
> > > 在07-3-31,jessinio smith < jessinio在gmail.com > 写道:
> > > >
> > > > __import__(...)
> > > >     __import__(name, globals, locals, fromlist) -> 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.
> > > >
> > > >
> > > >
> > > >
> > > > On 3/31/07, PianoPan < pianopan在gmail.com> wrote:
> > > > > 也就是根据变量的内容包含不同的文件,由运行时变量来决定要import 的文件的名字
> > > > > _______________________________________________
> > > > > python-chinese
> > > > > Post: send python-chinese在lists.python.cn
> > > > > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > > > > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > > > > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > > >
> > > > --
> > > > 注意身体,身体是革命的本钱!!
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese在lists.python.cn
> > > > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > > > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> > > >
> > >
> > >
> > >
> > > --
> > > powered by python
> >
> >
> >
> > --
> > powered by python
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> Linker M Lin
> linkerlin88在gmail.com
>   ※※※※※※※※※
>   ※※我思故我在※※
>   ※※※※※※※※※
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号