Imports with code generation working weirdly

If I run this as a script 100 times, approximately 60% of them it fails on import from dir2. This poses a problem for my test suite in my code generation framework where I generate code from previously imported code a lot. Note that it only affects my tests and I am not planning to do this in production.

But this makes testing really painful so I wish there was some solution. I tried disabling gc, running gc.collect before importing, sleeping – nothing really helped.

from pathlib import Path
import shutil

my = Path("my")

dir = my / "dir"

dir.mkdir(parents=True)
dir.joinpath("__init__.py").write_text("a = 7")
from my.dir import a

dir2 = my / "dir2"
dir2.mkdir()
dir2.joinpath("__init__.py").write_text("a = 20")

try:
    from my.dir2 import a

    print(a)
finally:
    shutil.rmtree(my)

Okay. Right after I gave up my search, I looked into importlib and found invalidate_caches which fixes my issue.

1 Like