3

After I read How do I clear all variables in the middle of a Python script? I used the code:

import sys
sys.modules[__name__].__dict__.clear()

and it removed all built-in names in python.

I tried to restore that by uninstalling my Anaconda and installing it again, but it seems that the problem cannot be fixed.

e.g. for my code:

ax.plot_surface(x1data2d , x2data2d , ydata2d, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

It shows the error:

NameError: name 'cm' is not defined

Do anyone know how to restore those built-in names?

MSeifert
  • 145,886
  • 38
  • 333
  • 352
Kong Chan
  • 33
  • 3
  • 1
    Does that problem persist even after restarting the Python interpreter? What is `cm` supposed to be anyway? Do you have stuff like `sum`, `map`, `abs`, etc.? – tobias_k Aug 11 '17 at 10:06
  • `cm` is not a builtin – Chris_Rands Aug 11 '17 at 10:06
  • 2
    Restarting the interpreter should fix this, but not without removing that rogue code from your module. – Moses Koledoye Aug 11 '17 at 10:11
  • 1
    `cm` isn't even a built-in, so I am unsure what relevance that has to the question. But clearly if you want to retain some names it wasn't an appropriate solution for you. What's the real problem you are trying to solve? – holdenweb Aug 11 '17 at 10:11
  • I now understand that restarting the interpreter can fix this. Thanks! – Kong Chan Aug 11 '17 at 10:46

1 Answers1

2

cm is not a built-in name.

For your code to work you have to import the cm module:

from matplotlib import cm

Note that sys.modules[__name__].__dict__.clear() is (almost) always a bad idea. If you don't want to clutter your "namespace" with lots of variables you are better of using modules and by avoiding any from some_package import * statements.

Note that you didn't have to reinstall Python after sys.modules[__name__].__dict__.clear() you just have to restart your interpreter. But please don't use that command - even the author of the answer said that it's not something he recommends:

I doubt you actually DO want to do this, because "every name" includes all built-ins, so there's not much you can do after such a total wipe-out. Remember, in Python there is really no such thing as a "variable" -- there are objects, of many kinds (including modules, functions, class, numbers, strings, ...), and there are names, bound to objects; what the sequence does is remove every name from a module (the corresponding objects go away if and only if every reference to them has just been removed).

MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • This won't work. The import mechanism went with `module.__dict__.clear()`. – Moses Koledoye Aug 11 '17 at 10:08
  • I was under the impression that he removed that from the code. But if it's still in the code you're completly right. – MSeifert Aug 11 '17 at 10:15
  • 1. The program runs successfully after I used that code. 2. But I remember I do not have this code before and the program give out a graph but this time not. 3. That's good news that I did not permanently lose the built-in name! Thanks! 4. I will avoid using that again. – Kong Chan Aug 11 '17 at 10:44
  • @KongChan Re 2) It could be that you used the `cm` from `matplotlib.pyplot` there, in most cases I've seen and used `plt.cm` instead of importing `cm` explicitly. However if it doesn't give you any "graph" that's likely something because of your "new anaconda installation" you might try to update `matplotlib` in your anaconda installation. (or you forgot to use `plt.show()` at the end?) – MSeifert Aug 11 '17 at 10:52