[IPython-user] "run" magic command bug
Sat Nov 24 22:32:59 CST 2007
On Oct 12, 2007 12:29 AM, Garaga Alexander <firstname.lastname@example.org> wrote:
> I found a problem with the "run" magic command which violates its documentation, namely this excerpt:
> The file is executed in a namespace initially consisting only of
> __name__=='__main__' and sys.argv constructed as indicated. It thus
> sees its environment as if it were being run as a stand-alone
> Here is my ipython session (uses pysh module):
> agaraga@nstmklel16[tmp]|3> cat ma.py
> agaraga@nstmklel16[tmp]|4> import ma
> agaraga@nstmklel16[tmp]|5> ma.a = 1000
> agaraga@nstmklel16[tmp]|7> cat 2.py
> import ma
> print ma.a
> agaraga@nstmklel16[tmp]|8> run 2.py
> As we see the changes made in the module "ma" namespace are actually visible from the program "2.py".
This isn't really a bug, though the docs could be more explicit. What
you are doing is modifying a global variable of the *entire* Python
interpreter (modules are stored once per Python VM instantiation, they
are fully global objects). So it's inevitable that you'll see this
side effect from different scripts.
What IPython does is to create an empty dict as your initial
namespace, but when you do imports, the actual module objects come
from Python's global module cache. There's nothing we can do about
that, it's just how the language works.
But thanks for your question, I added a clarification to the %run
docstring indicating this fact.
More information about the IPython-user