[IronPython] Loading Python modules from Silverlight Isolated Storage

Michael Foord fuzzyman at voidspace.org.uk
Tue Nov 4 11:42:13 CET 2008


Kenneth Miller wrote:
> It was proven to me that while exec might work for simple cases, more 
> advanced usage (declaring a lambda function inside an exec) can be 
> problematic. It was advised that I generate python modules and 
> dynamically import them. If memory served me, it's not possible to 
> import a module from a string object.
>
I have often generated (or loaded code into a string) and then exec'd it 
into a module dict to create modules at runtime. It is a relatively 
normal technique in Python.

Michael

> Thanks.
>
> - Ken
>
> On Nov 4, 2008, at 12:45 AM, "Curt Hagenlocher" <curt at hagenlocher.org 
> <mailto:curt at hagenlocher.org>> wrote:
>
>> You should be able to do this all in-memory:
>>
>> >>> import sys
>> >>> foo = type(sys)('foo')
>> >>> sys.modules['foo'] = foo
>> >>> exec 'a = 1' in foo.__dict__
>> >>> foo.a
>> 1
>> >>>
>>
>> There's probably a better ("more Pythonic") way to do this, but I'm 
>> sleepy...
>>
>> On Mon, Nov 3, 2008 at 11:40 PM, Kenneth Miller <xkenneth at gmail.com 
>> <mailto:xkenneth at gmail.com>> wrote:
>>
>>     All,
>>
>>       Is it possible to have python load modules from the silverlight
>>     isolated storage? My app needs to dynamically generate and import
>>     python code to run.
>>
>>     Regards,
>>     Ken
>>     _______________________________________________
>>     Users mailing list
>>     Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>>     http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog





More information about the Ironpython-users mailing list