I have 3 very simple scripts. The structure looks like this:
test.py
test_problem_folder
test_problem_1.py
test_problem_2.py
test.py:
import os
if __name__ == "__main__":
filename = "./test_problem_folder/test_problem_1.py"
exec(compile(open(filename, "rb").read(), filename, 'exec'), globals(), locals())
test_problem_folder/test_problem_1.py:
import test_problem_2
test_problem_2.test()
test_problem_folder/test_problem_2.py:
def test():
print("Hello")
If I try to run test.py, I get the error:
ModuleNotFoundError: No module named 'test_problem_2'
If I flatten the folder structure so that test_problem_* is the same directory as test.py, I don't get this problem. I figured the paths must be getting screwed up, so I tried os.chdir() to ./test_problem_folder, but that still gets the same error. What am I doing wrong? My real scenario is more complicated and I need to use exec instead of popen.
import .test_problem_2(note the leading.)