开发者

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

开发者 https://www.devze.com 2023-04-04 04:07 出处:网络
I just finished this exercise for beginners on creating and importing modules in开发者_如何转开发 python.

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?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号