0

My goal is to have a function with a list of strings as input and import those modules I want for my code.

Example:

list_of_modules = ['module1', 'module2']

def importer(list_of_modules):
    for module in list_of_modules:
        if module == 'module1':
            from package.package2.module1 import function1
        if module == 'module2':
            from package.package2.module2 import function1
        if module == 'module3':
            from package.package2.module3 import function1

I have tried this solution and it seems like the modules are not imported. I have also tried using import builtin function, but nothing.

My real example was:

def importer(modules):
    for value in modules:
        if value == "ble_connection":
            from top_app.module.connection.test_connectivity import connection1
        if value == "switch_connection":
            from top_app.module.connection.test_connectivity import connection2

I have to add that I am using pytest for running the tests from the general conftest file and this one calls this importer function during the pytest_configure hook which is right after the discovery hook.

Do you have any solution for this?

thanks

GGChe
  • 193
  • 1
  • 1
  • 11
  • 2
    This should work, albeit that function1 will be overwritten. – 9769953 Oct 08 '21 at 14:29
  • 2
    Alternatively, you can use the `importlib` module. – 9769953 Oct 08 '21 at 14:30
  • 1
    Can you add your attempt of using `__import__`? – Daweo Oct 08 '21 at 14:31
  • How have you tested that it's not working? Please show us your testing code. For example, a `print(function1)` line after the for loop would be enough. – 9769953 Oct 08 '21 at 14:31
  • I have tried __import__ but it looks like nothing is being imported – GGChe Oct 08 '21 at 14:43
  • Does this answer your question? [importing a module when the module name is in a variable](https://stackoverflow.com/questions/13598035/importing-a-module-when-the-module-name-is-in-a-variable) – Mathias R. Jessen Oct 08 '21 at 14:53
  • I guess that works for something like importing mathplotlib or scipy but I want to import custom packages like mypackage.module1.module2. I does not work for my case, or it seems not to be working – GGChe Oct 08 '21 at 15:16
  • If it works for standard packages, but not for your own custom packages, than it's likely the problem is with the structure of your package, and not with the import itself. – 9769953 Oct 10 '21 at 08:28
  • Thanks for the answer 976... – GGChe Oct 13 '21 at 14:36

1 Answers1

0

I would use exec for this purpose:

list_of_modules = ['module1', 'module2']
for module in list_of_modules:
    exec('package.package2.{} import function1'.format(module))

but as someone commented on your OP, function1 will be overwritten doing this.

lcdumort
  • 599
  • 3
  • 20
  • Thanks for the answer. I finally ended up using the pytest_collection_modifyitems hook from pytest. It works pretty well. Though it just allow you to deactivate certain test functions but packages are imported anyway. – GGChe Oct 13 '21 at 14:37