[파이썬] `sys.modules`: 이미 로드된 모듈

Python provides a powerful module called sys that allows you to interact with the Python runtime system. One of the useful features of the sys module is the ability to access information about the modules that have been loaded into memory during the program’s execution. In this blog post, we will take a closer look at the sys.modules attribute and how it can be used to access and manipulate loaded modules.

What are modules in Python?

Modules in Python are used to organize code into reusable and self-contained units. They can contain classes, functions, and variables that can be imported and used in other parts of your program. Modules provide a way to organize and structure code, making it more readable and maintainable.

Accessing loaded modules using sys.modules

The sys.modules attribute is a dictionary that contains information about all the modules that have been loaded into memory. The keys of this dictionary are the names of the modules, and the values are the module objects themselves. This attribute provides a convenient way to access and manipulate loaded modules during runtime.

To access a loaded module using sys.modules, you can simply use the name of the module as the key to retrieve the module object. Here’s an example:

import sys

# Accessing the 'math' module
math_module = sys.modules['math']

In the above example, we import the sys module and access the math module using sys.modules['math']. This will give us the module object for the math module if it has been loaded.

Checking if a module is loaded

You can use the sys.modules attribute to check if a module has been loaded into memory. You can simply use the in keyword to check if the module name is present in sys.modules. Here’s an example:

import sys

# Checking if the 'math' module is loaded
if 'math' in sys.modules:
    print("The 'math' module is loaded.")
else:
    print("The 'math' module is not loaded.")

Reloading a module

In some cases, you might want to reload a module to update its contents. Python provides the reload() function in the importlib module for this purpose. You can use reload() to reload a module by providing the module object as the argument. Here’s an example:

import sys
import importlib

# Reloading the 'math' module
math_module = sys.modules['math']
reloaded_math_module = importlib.reload(math_module)

In the above example, we import the importlib module and use importlib.reload() to reload the math module using the module object obtained from sys.modules['math'].

Conclusion

In this blog post, we explored the sys.modules attribute in Python, which allows you to access and manipulate loaded modules during runtime. We learned how to access a loaded module, check if a module is loaded, and reload a module if necessary. This attribute provides a flexible way to interact with loaded modules and can be particularly useful in scenarios where dynamic module loading or reloading is required.

Keep in mind that while manipulating loaded modules using sys.modules can be powerful, it is generally recommended to use it sparingly and only when necessary. Overusing or misusing this attribute can lead to code complexity and potential issues.