45

Possible Duplicate:
Unload a module in Python

After importing Numpy, lets say I want to delete/remove numpy import reference

import sys 
import numpy as np 

doMe()
   np.something()

#unimport np
#remove numpy from memory
Community
  • 1
  • 1
Merlin
  • 24,552
  • 41
  • 131
  • 206

1 Answers1

43

Unloading a module from Python is not supported.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 45
    `del sys.modules["moduleName"]` `del moduleName` – Miladiouss Jul 27 '18 at 22:37
  • Thanks @Miladiouss. That worked for me. – Cerin Aug 02 '18 at 21:40
  • 1
    It may have worked in one instance, but [this article](https://justus.science/blog/2015/04/19/sys.modules-is-dangerous.html) explains why it should be discouraged and can catastrophically break things. – Ben Aug 10 '18 at 22:12
  • 5
    So, the conclusion is that you should NEVER change your software once you write it? Write it absolutely correctly the first time so that you never have to change it? Debugging software is for amateurs? That makes NO sense! Instead of telling people what NOT to do, why not tell people how they SHOULD replace an old version of a module with a new version? – JDMorganArkansas May 08 '20 at 19:41
  • 7
    @JDMorgan No. That's not the conclusion. Just restart the process. Simples. – David Heffernan May 08 '20 at 21:21