0

I have several .py files in several folders. I do os.walk and get all names of .py files in list files and prior to that I know that each .py contains a function test() which takes an integer and returns an integer (which is somewhat unique). I am using the following code. Where r is an integer, files contain the path of a py file, for example:

C://users//codebase//user1//int_user_1.py

for fil in files:
    spec = importlib.util.spec_from_file_location("test", fil)
    foo = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(foo)
    get_test = foo.test(r)

But the problem here is the line spec.loader.exec_module(foo) is executing all the code in the file fil from line 1 of that particular .py file, but not just the function fil.test(). How can I execute only fil.test() function?

EDIT-1: I am unable to make any changes to the content of fil.py.

1 Answer 1

1

How can I execute only fil.test() function?

Wrap all the non-declaration code in an if __name__ == '__main__' condition.

Python modules are executable code, loading a module means executing the body of the module in a special "module namespace". If there's code you don't want executed on import, you have to "gate" it, using the condition above: a module's __name__ is only "__main__" if the module is being directly executed e.g.

$ echo "print(__name__)" > foo.py
$ python foo.py
__main__
$ python -c 'import foo'
foo
Sign up to request clarification or add additional context in comments.

5 Comments

Do you mean wrap all the non-declaration code in fil.py in if name == 'main' condition? Unfortunately I cannot do that as I don't have edit access to those files.
"Do you mean wrap all the non-declaration code in fil.py in if name == 'main' condition?" yes "Unfortunately I cannot do that as I don't have edit access to those files." well then you're hosed.
Nope, I managed this particular thing in the past, but unfortunately I forgot how I did that.
Only way I can see to do that would be to parse the file (without executing it), extract the function declaration you're interested in and eval that somehow, probably alongside all the imports declared in the file, and possibly a bunch of constants. So no, I don't think you're anything but hosed.
The line "def test():" is actually an assignment under the hood, kinda equivalent to the pseudocode "this_module.test = <function>". That assignment gets made by executing that line of code and the function body lines that follow. What you are actually asking for is a way to execute those lines of code without executing the file's other lines of code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.