Python Modules: When one imports them, do they go into memory?

*爱你&永不变心* 提交于 2019-12-30 17:43:35

问题


I just finished this exercise for beginners on creating and importing modules in python.

I was wondering does everything in the module get imported into the computer's memory?

Will there be implications later on with memory as the code gets longer and the modules imported become more numerous?

Will I need to know memory management to write resource-efficient code because of this?


回答1:


Your modules are automatically compiled (.pyc files) which are then imported into memory, but you do not have to be affraid of getting out of memory: modules are very small; it is common to have 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 of your code and modules in each module itself (and you should, read about docstrings here) without increasing the size or speed of the modules when loading, because the compiling-step takes out all unnecessary text, comments, etc.




回答2:


I can only imagine one way that imports could be abused to leak memory; You could dynamically create and import modules of arbitrary name (say, for the purpose of creating a plugin system); use them once and stop using them. If you did this through the normal import machinery, ie with __import__(variable_module_name), those modules would be added to sys.modules and even though they will not be used any further.

The solution is well, don't do that. If you are really creating a plugin system, then dynamic imports of that sort are probably fine, since the plugins would get re-used. If you really need to use dynamically generated, single use code; use eval.

If you really, really need to use importing on dynamically generated code (say, for automated testing), then you probably do need to poke around in sys.modules to erase the modules that you imported. Here's a nice article explaining how to do something like that.




回答3:


Yes and no.

Yes, the modules do get imported into the computers memory, but no you shouldn't be writing resource-efficient code because of this. Python modules are very small (a few KiB, in rare cases a few MiB) and have no significant effect on memory usage.



来源:https://stackoverflow.com/questions/7372966/python-modules-when-one-imports-them-do-they-go-into-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!