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?
When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.
Usually in /lib/site-packages in your Python folder. (At least, on Windows.) You can use sys. path to find out what directories are searched for modules.
For the most part, modules are just Python scripts that are stored in your Lib or Lib/site-packages folder, or local to the script being run. That's it. The installation of *most* modules is simply the moving of the module's files into these directories.
No, files can be imported from different directories.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With