Python 3.3+importlibfunction
importlib.invalidate_caches()
reloading and caches
Clears the module finders' caches so that import can see files created after the program started.
Syntax
importlib.invalidate_caches()
The module finder remembers the contents of sys.path directories and re-reads them by the directory's modification time, which has coarse resolution. If a program writes a .py file and imports it right away, the import may miss it — ModuleNotFoundError on a file that exists. Calling this before such an import fixes it; modules already in sys.modules are not reloaded — that is reload's job.
Examples
import importlib, sys
sys.path.insert(0, '.')
try:
import fresh_mod
except ModuleNotFoundError:
print('ещё нет') # → ещё нет
with open('fresh_mod.py', 'w') as f:
f.write('A = 5\n')
importlib.invalidate_caches() # без этого импорт может не увидеть файл
import fresh_mod
print(fresh_mod.A) # → 5
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy