Modification of files imported in Python using Importlib
While you feel the frustration of the importation of modules in your Python projects, it is great that you have already discovered the solution. In this article, we will immerse ourselves on how to modify the imported files along the powerful module Importlib
.
** What is “importlib”?
Importlib
is an integrated python module which provides a way to import and manipulate modules to execution. It allows you to load modules dynamically, without obliging them to be in the same package as your current module.
Basic example: Importing a module with Importlib
Let’s start with a simple example:
`Python
Matters
Define a personalized module (in this case, a function)
Def my_function ():
Print ("Hello from my_function!")
Try to import the module directly
To try:
my_Module = __import __ ("my_Module")
Except the importer:
Print ("Error: Impossible to find 'my_Module' Module ')
Now that we have the module, we can use its content
If hazattr (my_modle, "my_function"):
My_function ()
Other:
Print ("module 'my_Module' not found.")
'
In this example,__import __ (” My_Module “)Try to import the module
My_Module ‘. In case of success, attributes the result to a variable called “My_Module”. We then check if the attribute my_function
exists on this object using” hazattr (my_modle, “my_function”).
Modification of imported files
Now that we have configured an import, let’s take an additional step by modifying the files in the imported module. This is where things become really interesting.
`Python
Import my_module
Define a new function in our personalized module (in this case, another function)
Def New_function ():
Print ("Hello from new_function!")
Now we can import the modified module and use its contents
If hazattr (my_modle, "new_function"):
my_Module.new_function ()
Other:
Print ("module 'my_Module' not found.")
'
In this examination, we first import themy_Module ‘Using__imort __ (" My_Module ")
. We then define a new function in this module. If the function exists on the imported object (hazattr (my_modle," new_function ")
), it attributes the result to another variable called New_function '.
Example of use cases
Suppose we have a package with several modules, including one container our personalized functions:
Python
my_package /
__init__.py
my_Module.Py
'
"__Init __.My_Module.pycontains useful functions.
Modify themy_Module.pyfile to include a new function:
Python
Def my_new_function ():
Print ("Hello from my_new_function!")
__all__ = ["my_new_function"]
'
Now we can import and use this modified modified in our other code:
Python
de .my_package import *
New_function ()
'
The symbol* ‘matters all the attributes of the current module.
Conclusion
The modification of imported files using Importlib
is a powerful means of extending the features of your Python projects. By taking advantage of the syntax __imort __ (" module_name "), you can load modules dynamically and manipulate their content at the time of execution. This technique has many applications in fields such as automation, tests and even the construction of personalized tools.
Tips and tips
- UseImportlib.util.spec_from_file_location ()` to create a new module specification.
- Be careful when you use _ __import__ ‘with relative imports, as this can lead to circular dependencies.
- Keep your import instructions concise and focused on modules or specific packages.