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.joinpath("").write_text("a = 7")
from my.dir import a

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

    from my.dir2 import a


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

1 Like