0

I'm wondering about a few things concerning importing modules.
I have a module that contains nothing but a list of variables, so that I can use these across 3 or 4 scripts that run either once or daily.
This same module I would like to use in another script of mine, but I only need to load it once, and afterwards, I don't need the module anymore, because I would copy the variables to a list in my script(for comparison purposes).
My questions:
1. if I import the module in a method, is it discarded when the function ends? 2. what is the memory-impact on importing a module?

Good to know is that the function is one-shot.

Greetings

ShadowFlame
  • 2,996
  • 5
  • 26
  • 40

2 Answers2

2

A reference to the module is stored in sys.modules, so no it's not released.

Consider using execfile or similar if you don't want to load the module

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
2

You can actually unload a module from python, it will be garbage collected if it is not referenced anymore :

del sys.modules["mymodule"]
del mymodule
gbin
  • 2,960
  • 1
  • 14
  • 11