Skip to content

impossible to perform wire when modules are dinamically invoked with importlib.import_module #365

@Balthus1989

Description

@Balthus1989

Hello there,
I noticed that an explicit import of a package works smoothly, while a dinamic import causes the wire function to not work properly

When it works:

`from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide
from pathlib import Path

from src.service.costestimator.costestimator import costestimator as CE

if name == 'main':
service = 'costestimator'
customer = 'john_doe'
plant = '0'

# Retrieving paths
root_dir = Path.cwd().parent
ini_file = service + '_' + customer + '_' + plant + '.ini'
ini_path = root_dir.joinpath(root_dir, 'config', 'params', ini_file)

# Loading needed libraries
container = importlib.import_module('src.service.' + service + '.configuration.configuration')
configuration = container.Container()
configuration.config.from_ini(ini_path)
configuration.wire(modules=[sys.modules[__name__]])

CE_1 = CE()`

In this case, the output is constituted by prints of fields coming from an .ini file (i.e.: the desired output).

In the following case, it does not work:
`
from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide
from pathlib import Path
import importlib

if name == 'main':
service = 'costestimator'
customer = 'john_doe'
plant = '0'

# Retrieving paths
root_dir = Path.cwd().parent
ini_file = service + '_' + customer + '_' + plant + '.ini'
ini_path = root_dir.joinpath(root_dir, 'config', 'params', ini_file)

# Loading needed libraries
container = importlib.import_module('src.service.' + service + '.configuration.configuration')
configuration = container.Container()
configuration.config.from_ini(ini_path)
configuration.wire(modules=[sys.modules[__name__]])

getattr(importlib.import_module('src.service.' + service + '.' + service), service)()

`

in this case, the result is a print of the Provide objects.

The init function of the class costestimator is written as:
class costestimator(): def __init__(self, customer_name = Provide[Container.config.customer.customer_name.as_(str)], customer_plant = Provide[Container.config.customer.customer_plant.as_(str)]):

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions