4

Does this code, or similar, allow for 'something' to be picked up by garbage collection after it has been dereferenced?

import something as this

if condition:
    import somethingelse as this

I want to import different modules to replace others to the same alias depending on conditions, and not take up loads of memory with all the modules being 'loaded' at the same time

Thanks

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
holmeswatson
  • 969
  • 3
  • 14
  • 39

2 Answers2

2

No, the original module is still cached in sys.modules as sys.modules['something']. It is generally quite difficult to fully unload a module; see Unload a module in Python.

Community
  • 1
  • 1
ecatmur
  • 152,476
  • 27
  • 293
  • 366
0

Loaded modules are cached so eliminating your reference does not eliminate all references. It will not be garbage collected.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622