classification
Title: importlib sometimes fails to import a recently created module
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.3, Python 3.4, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, brett.cannon, eric.snow, ncoghlan, rjdymond
Priority: normal Keywords:

Created on 2015-02-08 15:43 by rjdymond, last changed 2015-02-09 15:45 by brett.cannon. This issue is now closed.

Files
File name Uploaded Description Edit
importerror.py rjdymond, 2015-02-08 15:43
Messages (3)
msg235557 - (view) Author: Richard Dymond (rjdymond) Date: 2015-02-08 15:43
importlib.import_module() sometimes fails to import a module that has just been written to the filesystem, aborting with an ImportError.

Example output when executing the attached file with Python 3.3 or 3.4:

Wrote tmpwbzb35.py
  Successfully imported tmpwbzb35
Wrote tmp34c6qs.py
Traceback (most recent call last):
  File "importerror.py", line 12, in <module>
    importlib.import_module(modname)
  File "/home/rjd/Python/Python3.3/lib/python3.3/importlib/__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1577, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1558, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1522, in _find_and_load_unlocked
ImportError: No module named 'tmp34c6qs'

I have been unable to reproduce this behaviour with Python 2.7 or Python 3.2.
msg235559 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2015-02-08 16:03
Looks like some sort of race condition. Sleeping for a second before import_module seem to solve the problem.
msg235617 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2015-02-09 15:45
Because stat results are cached for performance reasons, it's possible to write to the file system and then try to read/import from it before the modification time for the directory even picks up that there was a change. For this reason there is importlib.invalidate_caches(): https://docs.python.org/3/library/importlib.html#importlib.invalidate_caches .

I have opened http://bugs.python.org/issue23422 to track that the docs for importlib.import_module() point out invalidate_caches().
History
Date User Action Args
2015-02-09 15:45:49brett.cannonsetstatus: open -> closed
resolution: not a bug
messages: + msg235617
2015-02-08 16:22:02SilentGhostsetversions: + Python 3.5
2015-02-08 16:03:43SilentGhostsetnosy: + eric.snow, SilentGhost, brett.cannon, ncoghlan
messages: + msg235559
components: + Interpreter Core
2015-02-08 15:43:35rjdymondcreate