Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

%run modules clobber each other #3547

Closed
takluyver opened this Issue Jul 5, 2013 · 2 comments

Comments

Projects
None yet
2 participants
Owner

takluyver commented Jul 5, 2013

I have used %run to define functions from two different importable modules in the same notebook (I know, I should use import, but %run is really convenient when you're still changing the file). When I %run each, it seems to clobber the other, so when I call the function from that one, it gives me NameErrors (not for the function itself, but for things referenced in the function).

Also, the traceback goes a bit funny - e.g. when I call violin_plot() from violinplot.py, having run average.py more recently, it says that the stack frame is in ...scripts/average.py in violin_plot(data1, classes, data2, **kwargs).

I suspect that the problem is in the machinery that caches module namespaces. And I have a feeling that I'm the last one to have delved into that, so I'll try to investigate it at some point.

Owner

minrk commented Jul 5, 2013

There are a few too many nuances for me to fully grok, can you include a working example?

Owner

takluyver commented Jul 5, 2013

Here's a minimal example, I'm just about to look into it.

$ cat a.py 
avar = 1

def afunc():
    return avar
$ cat b.py
bvar = 2

def bfunc():
    return bvar
$ ipython
...

In [1]: %run a

In [2]: afunc()
Out[2]: 1

In [3]: %run b

In [4]: bfunc()
Out[4]: 2

In [5]: afunc()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-c4f77d893f1d> in <module>()
----> 1 afunc()

/home/thomas/scratch/a.py in afunc()
      2 
      3 def afunc():
----> 4     return avar

NameError: global name 'avar' is not defined

The traceback is correct this time: I'm not sure what's changed there.

@takluyver takluyver added a commit to takluyver/ipython that referenced this issue Jul 5, 2013

@takluyver takluyver Add failing test for gh-3547 601235a

@takluyver takluyver added a commit to takluyver/ipython that referenced this issue Jul 5, 2013

@takluyver takluyver Simplify caching of modules from %run-ing scripts.
Previously, we cleared and re-used a single FakeModule instance in which to
run cells, and cached copies of the modules' namespaces to prevent them from
being cleared. Now, we cache one FakeModule instance per script file, clearing
it and re-using it if the same script is re-run. This seems more robust.

Closes gh-3547
279211b

@fperez fperez closed this in #3555 Jul 11, 2013

@fperez fperez added a commit that referenced this issue Jul 11, 2013

@fperez fperez Merge pull request #3555 from takluyver/i3547
Simplify caching of modules with %run.

Previously, we cleared and re-used a single FakeModule instance in which to run scripts, and cached copies of the modules' namespaces to prevent them from being cleared. Now, we cache one FakeModule instance per script file, clearing it and re-using it if the same script is re-run.

Closes #3547, and fixes another test that was marked as a known failure.
6217a47

@mattvonrocketstein mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014

@takluyver takluyver Add failing test for gh-3547 06b4943

@mattvonrocketstein mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014

@takluyver takluyver Simplify caching of modules from %run-ing scripts.
Previously, we cleared and re-used a single FakeModule instance in which to
run cells, and cached copies of the modules' namespaces to prevent them from
being cleared. Now, we cache one FakeModule instance per script file, clearing
it and re-using it if the same script is re-run. This seems more robust.

Closes gh-3547
360e87a

@mattvonrocketstein mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014

@fperez fperez Merge pull request #3555 from takluyver/i3547
Simplify caching of modules with %run.

Previously, we cleared and re-used a single FakeModule instance in which to run scripts, and cached copies of the modules' namespaces to prevent them from being cleared. Now, we cache one FakeModule instance per script file, clearing it and re-using it if the same script is re-run.

Closes #3547, and fixes another test that was marked as a known failure.
6dbf16d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment