Skip to main content
3 of 4
added 498 characters in body
Rohi
  • 804
  • 1
  • 10
  • 26

Dynamically importing an entire module

I was wondering if there was a way to dynamically import an entire module (equivalent to "import * some_module").

After some research on other posts I saw you could dynamically do "import some_module", but not pull the entire module.

In order to clarify my question,

some_module :

SOME_VARIABLE = 1 

module a :

if(1 == 1): __import__("some_module"); 

module b :

from module a import * print(SOME_VARIABLE); 

running this code returns an unrecognized variable exception, I would like to prevent that.

Motiviation :

Lets say I have a global module which imports multiple other module via a function that recieves an argument.

Example :

module a :

import_modules(modules_id): if(modules_id == 1): from module_c import * from module_d import * else : from module_e import * 

module b :

from module a import * import_modules(1); 

and I want to access all variables from module_c/d with explicit calling.

If it is not possible I would appreciate if some one could explain why not.

Rohi
  • 804
  • 1
  • 10
  • 26