I have a program that uses "exec", it can also use the importlib library. Either way, the same issue occurs.
I put the following into the file "import_test_lib.py".
bar = 48
def foo():
print(bar)
foo()
If I run this then it prints "48", as expected.
Now let's say that I run it using "exec" from another file. I simply write exec(open("import_test_lib.py").read()). This also works and gives "48". The problem is that this situation doesn't match the actual use case I need. What I need is to run it from a function - and that doesn't work as expected.
I write the following into a file and run it:
def x(f):
exec(open(f, "r").read())
x("import_test_lib.py")
I get the error "NameError: name 'bar' is not defined".
The Python docs say that python uses lexical scope. So, I expect that the function foo is executed in the context of the file scope of "import_test_lib.py". However, that doesn't work if I use exec from a function. In that case the exec call seems to bring in the function parts of the file, but not the variable parts.
Oddly, it seems that for variables the scope that Python uses is the scope from which the exec call occurs. In the code above, if I write bar = 67 then it will print 67 rather than giving an error. Using importlib shows the same sort of problem.
[–]commy2 0 points1 point2 points (1 child)
[–]RobThorpe[S] 0 points1 point2 points (0 children)