0

I want to execute a piece of Python code [B] from my existing Python code [A]. In B I would like to access methods defined in A. From A I would like to pass arguments to B.

If I try this way:

B.py

def Main(args):
   print(args)
   method_a()  # this doesn't work

A.py

def method_a():
   return 5
mod = importlib.import_module('B')
mod.Main(123)

Using import_module I do not have access to methods in A.py

If I use eval then I cannot pass any arguments to it and I'm not able to execute an entire file: eval('print(123)') works but eval(open('B.py').read()) does not.

If I use exec I can access methods from A.py but I cannot return any value or pass any arguments. I'm Using python 3.6

5
  • Why do you want this? I'm 100% certain that there is a better way to accomplish what you actually need. Commented Mar 31, 2020 at 6:37
  • Why don't you just import A in B.py??? And yeah, you coudl use importlib.import_module, but that usually isn't the best, in any case though, if you did, you *definitely could use the functions in A.py. But importlib.import_module should work. Commented Mar 31, 2020 at 6:43
  • @Selcuk could you elaborate what other ways are there for me to execute python code? Commented Mar 31, 2020 at 6:46
  • Oh, I think I see, you mean method_a doesn't work. in B.py But of course not, and dynamically executing code isn't going to help that. method_a() as used in B.Main requires method_a to be in the global scope of the module. So, you could add method_a to the module object, but that is really not a reasonable thing to be doing. It seems like you should just change your approach Commented Mar 31, 2020 at 6:47
  • So Python, like pretty much all modern langauges, uses lexical scoping, which seems to be your fundamental issue. You want dynamic scoping. There's no way to do that other than ugly hacks like mod.method_a = method_a; mod.Main(123) in A.py, instead, Main could take the function as a parameter, that is how you should be communicating with a function from the caller. Not by hacking module namepsaces. Or again, just a re-design. Why are you trying to do it this way? Commented Mar 31, 2020 at 6:50

1 Answer 1

3

Wrong design - two modules (or a module and a script - the only difference is how it's used) should not depend on each other. The correct way to structure your code is to have your "library" code (method_a etc) in one (or more) modules / packages that are totally independant of your application's entry point (your "main" script), and a main script that depends on your library code.

IOW, you'd want something like this:

# lib.py

def method_a(args):
    print(args)
    return 42

def method_b():
    # do something

# etc

And

# main.py

import sys
import lib

def main(args):
    print(lib.method_a(args))

if __name__ == "__main__":
    main(sys.argv)
Sign up to request clarification or add additional context in comments.

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.