<HTML><BODY style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; "><DIV>almost... but I want to be able to save c and reload it before the exec. It appears c cannot be picked.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Massimo</DIV><BR><DIV><DIV>On Aug 29, 2007, at 2:28 AM, Cosmin Stejerean wrote:</DIV><BR class="Apple-interchange-newline"><BLOCKQUOTE type="cite">I'm not sure if I understand what's going on with the environment = {} part so I'm not sure if my solution to your problem is correct but here is what I think is wrong. As far as I can tell exec can take either a string or a code object. exec(open(' a.py','r').read()) works because you are reading the code from a file (it could have been any other file extension) and exec is parsing it.<BR><BR>exec(open('a.pyc','r').read()) this however reads the bytecode as a string and passes it to exec. This is probably not what you want (and most likely won't work). One solution is to simply import a, but I'm guessing that's not what you want either. The other option is to not use py_compile but use the compile function and pickle as follows <BR><BR>c = compile("print x", '&lt;string&gt;', 'exec')<BR><BR>now you have a code object that you can execute<BR><BR>exec(c)<BR><BR>If you pickle this code object to a file you will be able to read it and run it (then again why not pickle the original string??). The problem is pickle won't pickle code objects. But apparently you can use a little trick around that (see <A href="http://mail.python.org/pipermail/python-list/2002-August/161661.html">http://mail.python.org/pipermail/python-list/2002-August/161661.html</A>)<BR><BR>Is this at least close to what you were looking for?<BR><BR>- Cosmin <BR><BR><DIV><SPAN class="gmail_quote">On 8/29/07, <B class="gmail_sendername">Massimo Di Pierro</B> &lt;<A href="mailto:mdipierro@cti.depaul.edu" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">mdipierro@cti.depaul.edu </A>&gt; wrote: </SPAN><BLOCKQUOTE class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><BR>Hello,<BR><BR>I am hoping one of you can help me with this.<BR><BR>EXAMPLE:<BR><BR> environment={'x':3}<BR>open('a.py','w').write('print x')<BR>import py_compile,os<BR>exec(open('a.py','r').read()) in environment ### prints 3 OK!<BR>py_compile.compile(' a.py')<BR>os.unlink('a.py')<BR>exec(open('a.pyc','r').read()) in environment ### does not work<BR><BR>QUESTION: how do exec a.pyc into environment so that it prints 3? Is<BR>it possible to do it? <BR><BR>Massimo<BR>_______________________________________________<BR>Chicago mailing list<BR><A href="mailto:Chicago@python.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">Chicago@python.org</A> <BR><A href="http://mail.python.org/mailman/listinfo/chicago" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://mail.python.org/mailman/listinfo/chicago </A><BR></BLOCKQUOTE></DIV><BR><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">_______________________________________________</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Chicago mailing list</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><A href="mailto:Chicago@python.org">Chicago@python.org</A></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><A href="http://mail.python.org/mailman/listinfo/chicago">http://mail.python.org/mailman/listinfo/chicago</A></DIV> </BLOCKQUOTE></DIV><BR></BODY></HTML>