Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importlib sometimes fails to import a recently created module #67600

Closed
rjdymond mannequin opened this issue Feb 8, 2015 · 3 comments
Closed

importlib sometimes fails to import a recently created module #67600

rjdymond mannequin opened this issue Feb 8, 2015 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@rjdymond
Copy link
Mannequin

rjdymond mannequin commented Feb 8, 2015

BPO 23412
Nosy @brettcannon, @ncoghlan, @ericsnowcurrently
Files
  • importerror.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2015-02-09.15:45:49.948>
    created_at = <Date 2015-02-08.15:43:35.922>
    labels = ['interpreter-core', 'type-bug', 'invalid']
    title = 'importlib sometimes fails to import a recently created module'
    updated_at = <Date 2015-02-09.15:45:49.947>
    user = 'https://bugs.python.org/rjdymond'

    bugs.python.org fields:

    activity = <Date 2015-02-09.15:45:49.947>
    actor = 'brett.cannon'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-02-09.15:45:49.948>
    closer = 'brett.cannon'
    components = ['Interpreter Core']
    creation = <Date 2015-02-08.15:43:35.922>
    creator = 'rjdymond'
    dependencies = []
    files = ['38046']
    hgrepos = []
    issue_num = 23412
    keywords = []
    message_count = 3.0
    messages = ['235557', '235559', '235617']
    nosy_count = 5.0
    nosy_names = ['brett.cannon', 'ncoghlan', 'SilentGhost', 'eric.snow', 'rjdymond']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue23412'
    versions = ['Python 3.3', 'Python 3.4', 'Python 3.5']

    @rjdymond
    Copy link
    Mannequin Author

    rjdymond mannequin commented Feb 8, 2015

    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.

    @rjdymond rjdymond mannequin added the type-bug An unexpected behavior, bug, or error label Feb 8, 2015
    @SilentGhost
    Copy link
    Mannequin

    SilentGhost mannequin commented Feb 8, 2015

    Looks like some sort of race condition. Sleeping for a second before import_module seem to solve the problem.

    @SilentGhost SilentGhost mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Feb 8, 2015
    @brettcannon
    Copy link
    Member

    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().

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant