0

I have a file with all the modules that I use for my project:

# setup.py
import A
import B
import C

I then call these modules from a different file as:

from setup import *

Suppose I now have a list of keys for these modules:

my_modules = ['A', 'B', 'C']

I wanto import them in my setup.py file dynamically. So I used:

import importlib
for key in my_modules:
    importlib.import_module(key)

My problem is that this doesn't let me use the names in my main script:

from setup import * # imports A, B and C but no global name is assigned.

A.some_function()

...returns that the module is not defined. How can I make the "true" name of the modules be available globally?

  • 1
    `importlib.import_module` *returns* the imported module, and you need to assign it to a name. Which you could do dynamically, see the duplicate. But really, imports should be explicit, not hyper dynamic, or you can get hard to trace issues. Even `import *` is frowned upon, as you can't be sure what exactly you're importing. – deceze Jan 21 '22 at 14:47

0 Answers0