I've deleted a (package builtin) function on ipython:
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import math
In [2]: math.cos(0)
Out[2]: 1.0
In [3]: del math.cos
In [4]: math.cos(0)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-9cdcc157d079> in <module>()
----> 1 math.cos(0)
AttributeError: module 'math' has no attribute 'cos'
OK. But how do I reload the function? This didn't help:
In [5]: import importlib
In [6]: importlib.reload(math)
Out[6]: <module 'math' (built-in)>
In [7]: math.cos(0)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-9cdcc157d079> in <module>()
----> 1 math.cos(0)
AttributeError: module 'math' has no attribute 'cos'
OK people, I wrote a little module to solve this confusion. I sincerely doubt that it is buggless. So improvements are welcome.
This module works nicely both on Py 2 and 3. Tried on Python 2.7 and Python 3.5.
The above code works for me in Python 3.4 on Windows but the documentation for 3.6 states:
(so maybe I was only "lucky")
so what is pretty sure to work is:
It still works, and you don't even need
reload
. Just remove from cache & import again.As noted in comments, using
reload
also works, but you need to update the module reference given byreload
, not just reuse the same old one with thecos
entry missing: