New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to preserve user variables from import clashing? #2664
Comments
Another option is to configure pylab to not import anything into the global namespace What pylab imports does is essentially:
It is the last line that gives the problem and can be removed by the configure variable. If you just want an eventloop integration you can also use the %gui magic instead of %pylab pylab_import_all can also be configured from inside IPython using the %config magic |
Thanks @jenshnielsen for the nice answer. There isn't anything else we can do here. |
I guess one thing we could do is extend the |
@jenshnielsen, @bfroehle, thanks for your suggestions but I think that would defeat %pylab purpose Checking can be done, IMHO. Pylab magic imports matplotlib and numpy, so we have their namespaces. |
The relevant bit of code is at https://github.com/ipython/ipython/blob/master/IPython/core/pylabtools.py#L238 I take it you would advocate for something like: s = ("from matplotlib.pylab import *\n"
"from numpy import *\n")
pylab_ns = {}
exec s in pylab_ns
for key in user_ns:
pylab_ns.pop(key, None)
user_ns.update(pylab_ns) Personally, I think variable the clobbering is to be expected. |
Not exactly like that. import matplotlib.pyplot as plt
import numpy as np
[x for x in dir(plt) + dir(np) + dir(np.fft) + dir(np.random) + dir(np.linalg) if x.islower() and not x.startswith('_')] that is executed before importing pylab anyway, will give us a list of names that could potentially crash user variables. Then just do the matching before importing pylab |
There are multiple purposes of %pylab. To me the most important is that it enables a gui eventloop for interactive plotting and sets matplotlib in interactive mode. The imports only save a few lines of code. To me the I don't think filtering on import is a good idea. But adding a warning to pylab is not a bad idea, something like "warning pylab imports has overwritten the local variable f with the function f". It could also be a good idea if %pylab would print what imports is does like isympy |
I'd agree with the others in this thread - code that magically detects what variables you already have and modifies its own behaviour sounds like a recipe for surprise and confusion. I agree that it would be good for |
I left a space that this may be too subjective, but I'll reply as I believe it's right way to do it
Suggested behavior is about special pylab construction, it's not about arbitrary known workflows. Enhanced interactive environment as IPython is, I believe should take care about this special case as IPython is one great example to support the pylab idea.
I can't make the meaning of this |
No, I still think, even for By 'show what it has imported', I mean that the message it displays should say something like:
|
But pylab IS special case. |
I agree that it's a special case, I just don't think it's special enough to On 7 December 2012 12:11, klonuo notifications@github.com wrote:
|
I agree that having %pylab not import things because they are already in the namespace is the wrong idea. This will break notebooks that might require (even implicitely or because of a temporary variable) on the fact that %pylab does overwrite variable. That it warns of overwrite in user_ns why not. |
As @Carreau described, I've certainly used the clobbering purposefully a few times. For example, if I accidentally define a variable named To reiterate a few previous ideas:
Quick question:Would there be any side effects to switching exec pylab_import_string in user_ns to pylab_ns = {}
exec pylab_import_string in pylab_ns
user_ns.update(pylab_ns) This would allow us to build a list of clobbered variables as clobbered = set(pylab_ns).intersection(user_ns) Note that this would get very annoying if one ran |
Yesterday I opened issue in MPL project: pylab overwrites user variable(s) which I wanted to open first here, but then thought that perhaps MPL is more appropriate.
Issue is closed, and from the comments it may be reasonable. I don't know if this is highly subjective, but the issue I had seemed as a bad design to me - as mentioned, I had
f
hdf5 file object that was connected to couple of numpy arrays which just all disappeared after I entered %pylab mode, as file handle was reassigned.Suggested approaches as entering %pylab before doing else, or don't name your variable as
f
or any other name present in %pylab space, aren't good enough, obviously.There are no other lower case, one letter variables that can be overwritten except
f
ande
in such scenario. And also maybe some other potential handy named variables that user may assign and are exposed in %pylab mode.Does IPython have instrument to avoid similar clashing with variable names in current user namespace?
The text was updated successfully, but these errors were encountered: