0

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.

My actual problem :

I have an interface driver which interacts with different devices using different protocols (Each implemented via a different class), these protocols have certain similarities, for example : they both a have a CUSTOMER_STATUS dict (Which is defined in a protcol_a/b_global_def module which is imported by the specific protocol).

The problem I am encountering is that both protocols have variables with the same name, and since the interface driver needs to recognize them both I cant know which variable I am accessing from the program that is using the interface driver (Imports it).

Take in account that the program using the interface driver only knows dynamically which protocol it needs to use and I want to avoid importing the protocol global def manually inside the program (Even though I want to use its variables in the program).

9
  • 2
    Show your code, so that we can refer to your question properly. Commented Mar 11, 2018 at 9:50
  • My question isn't specific to a certain piece of a code, although I could explain the motivation for doing this if you feel it matters. Commented Mar 11, 2018 at 9:55
  • Well then your question doesn't make much sense. Of course you can import dynamically. Part of a module or the entire module, whatever you choose. So either give a counterexample (in code), or rephrase it. Commented Mar 11, 2018 at 9:59
  • Hey I have rephrased it adding an example. Commented Mar 11, 2018 at 10:06
  • Why not? Your code will be more insecure, and difficult to maintain. But with python, practicability is better then purity, so there could be useful cases (which I cannot see now). Commented Mar 11, 2018 at 10:12

3 Answers 3

2

This is almost certainly an XY problem ... but to demonstrate it can be done ...

Suppose some_module is

SOME_VARIABLE = 1

Make the module a be:

globals().update(__import__("some_module").__dict__)

Now in b

from a import *
print(SOME_VARIABLE)

will produce your desired result.

What is happening here? In a we import some_module, then take its __dict__ which represents its namespace, and use it to update the globals in a.

Sign up to request clarification or add additional context in comments.

3 Comments

I had globals().update(vars(__import__('module_c'))) but yeah... this'll clobber the namespace, probably lead to confusion with regards to caching of module imports and other things I'm trying not to think too hard about... but it does what the OP asked for - even if like you I strongly agree this is probably an XY problem :)
Im afraid both solutions are not working for me. Ill explain what im trying to do in order to clear this up as a XY problem.
I have updated the post to include my actual problem, I hope its clarified enough if not tell me and ill try to rephrase. Thanks ahead of time.
1

module a

exec('from some_module import *')

module b

import importlib
a=importlib.import_module('module a')
for i in dir(a):
    if not i.startswith('_'):
        exec('{0}=a.{0}'.format(i))
print(SOME_VARIABLE);

5 Comments

This is not what I was looking for, I re-edited the question in order to give a better understanding of the question.
Seeing as I want module a to do the importing for module b, this isn't working.
@Rohi what do you get
I should of stated that more clearly, its not that it isn't working its the fact that its not solving my problem. I want module b, to only import module a, and for module a to import everything else for module b (Without adding extra logic in module b).
@Rohi is it necessary to contain space in your module name
0

Extension to donkopotamus's answer

Make the module a be:

def load_module():
    globals().update(__import__("some_module").__dict__)

Now in b

from a import *
load_module()
from a import *
print(SOME_VARIABLE)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.