In this test, I create a new directory (deleting it if it already exists), add it to sys.path
, add two simple Python files, and try to import them. I’m using Python 3.11.6.
import sys
from pathlib import Path
from shutil import rmtree
d = Path('/tmp/fleen')
rmtree(d)
d.mkdir()
sys.path = [str(d)] + sys.path
(d / 'module1.py').write_text('x = 1')
import module1
assert module1.x == 1
(d / 'module2.py').write_text('x = 2')
print('file contains:', repr((d / 'module2.py').read_text()))
import module2
assert module2.x == 2
This prints file contains: 'x = 2'
, as expected, but then raises
Traceback (most recent call last):
File "example.py", line 16, in <module>
import module2
ModuleNotFoundError: No module named 'module2'
What’s going on? Why can’t import
see the file? Is there some sort of cache of each directory in sys.path
that I need to clear?