__import__ with dict values

alex goretoy aleksandr.goretoy at gmail.com
Thu Mar 12 21:00:30 EDT 2009


Gabriel,

Thank you. This makes sense to me. I will go with sys.modules. Can you give
me a good example how to do it getattr way?

currently I am having this problem in my code. Kinda off subject, but not
entirely. I set default variable in self.opt after that I import
jar.properties into self.opt['properties']. Now my self.opt doesn't have the
same defaults set. in other words we load our configuration properties. Then
we over write any configuration properties with supplied sys.argv[1::]
arguments I am passing my sys.argv to a class and inside the class I use
getopt to get site_name and files_name, also many other variable that
overwrite the configuration that is set from the jar.properties

This was working when I was using exec and eval. I was not able to just use
exec I had to use exec on import and eval on module.module it was wierd, can
someone tell me why?

-Alex Goretoy
http://www.goretoy.com



On Thu, Mar 12, 2009 at 1:00 PM, Gabriel Genellina
<gagsl-py2 at yahoo.com.ar>wrote:

> En Thu, 12 Mar 2009 09:27:35 -0200, alex goretoy <
> aleksandr.goretoy at gmail.com> escribió:
>
>  note i would still like to be able to do __import__("sys")."path"
>>>
>>
> p = __import__("sys").path
>
> That's a convoluted way of doing:
>
> import sys
> p = sys.path
>
> (except that the latter one inserts "sys" in the current namespace)
>
>  maybe if __import__ had __str__ defined, How is my thinking on this?
>>> and how would I achieve something like this?
>>>
>>
> __str__ has absolutely nothing to do.
>
>  __import__(opt['imp_mod']).options
>>
>> eval(opt['imp_mod']+"."+opt['imp_opt'])
>>
>> how to make top work like bottom?
>>
>
> If you think you have to use eval: you don't. Never.
>
> module = __import__(opt['imp_mod'])
> module.options
>
> If the name "options" is not known until runtime, use getattr:
>
> getattr(module, name_of_attribute)
>
> The above assumes you want an attribute (like logging.ERROR). If you want a
> sub-module (a module inside a package) use __import__("dotted.name") and
> then retrieve the module by name from sys.modules; see
> http://docs.python.org/library/functions.html#__import__
>
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090312/76da98db/attachment-0001.html>


More information about the Python-list mailing list