Python modules: when they import them, do they get into memory?

I just finished this beginner exercise when creating and importing modules in python.

I was wondering if everything in the module is imported into the computer’s memory?

Will there be consequences later with memory, as the code becomes longer and the imported modules become more numerous?

Do I need to know memory management in order to write resource-efficient code because of this?

+7
source share
3 answers

Your modules are automatically compiled (.pyc files), which are then imported into memory, but you do not need to be afraid to leave the memory: the modules are very small; there are usually thousands of modules loaded at a time!

You do not need to know memory management as Python does all the work for you.

edit: You can also write a lot of documentation about your code and modules in each module (and you should read the docstrings here ) without increasing the size or speed of the modules when loading, because unnecessary text, comments, etc. are extracted at the compilation stage.

+6
source

I can only imagine how imports could be abused to leak memory; You can dynamically create and import arbitrary name modules (for example, to create a plug-in system); use them once and stop using them. If you did this using the usual import mechanism, i.e. Using __import__(variable_module_name) , these modules will be added to sys.modules and will not even be used further.

The solution is good, do not do it. If you are really creating a plugin system, then dynamic imports of this kind are probably great, as plugins will be reused. If you really need to use a dynamically generated one-time code; use eval .

If you really really need to import dynamically generated code (say, for automatic testing), then you probably need to poke into sys.modules to erase the modules you imported. Here is a good article explaining how to do this.

+3
source

Yes and no.

Yes, the modules are indeed imported into the computer’s memory, but no, you should not write resource-saving code because of this. Python modules are very small (several KiB, in rare cases, several MiB) and do not significantly affect memory usage.

0
source

All Articles