I get unexpected behaviour when I try to run methods defined in a file loaded using the runpy module. The methods do not see any variables (including imported modules) defined outside of that method. Here is how I am doing it:
#test.py
import runpy
env = runpy.run_path('test', {'y':'world'})
env['fn']()
~
#test
import re
print(re.compile(r'^hello', re.IGNORECASE).sub('', "hello world"))
x = "hello"
print(x)
print(y)
def fn():
try:
print(re.compile(r'^hello', re.IGNORECASE).sub('', "hello world"))
except:
print("No re")
try:
print(x)
except:
print("No x")
try:
print(y)
except:
print("No y")
My expected output of test.py would be:
world
hello
world
world
hello
world
because fn would form a closure for re, x and y.
However, instead I get:
world
hello
world
No re
None
None
It looks like re isn't defined within fn even though it should be with normal closure behaviour. x and y are even stranger because they appear to be defined but set to None.
Why is this and how do closures work with runpy? How can I achieve normal behaviour such that fn can 'see' outside variables?