Skip to content

Import module doesn't updated #85936

Closed
Closed

Description

@cheny0y0
mannequin
BPO 41770
Nosy @terryjreedy, @stevendaprano, @cheny0y0

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 2020-09-18.23:09:22.379>
created_at = <Date 2020-09-12.03:00:14.826>
labels = ['interpreter-core', 'invalid', 'type-bug', '3.8']
title = "Import  module doesn't updated"
updated_at = <Date 2020-09-18.23:09:22.378>
user = 'https://github.com/cheny0y0'

bugs.python.org fields:

activity = <Date 2020-09-18.23:09:22.378>
actor = 'terry.reedy'
assignee = 'none'
closed = True
closed_date = <Date 2020-09-18.23:09:22.379>
closer = 'terry.reedy'
components = ['Interpreter Core']
creation = <Date 2020-09-12.03:00:14.826>
creator = 'prasechen'
dependencies = []
files = []
hgrepos = []
issue_num = 41770
keywords = []
message_count = 5.0
messages = ['376766', '376767', '376770', '376818', '377142']
nosy_count = 3.0
nosy_names = ['terry.reedy', 'steven.daprano', 'prasechen']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue41770'
versions = ['Python 3.8']

Activity

  1. cheny0y0 commented on Sep 12, 2020

    cheny0y0mannequin
    MannequinAuthor
    # I create a module:
    # \sys.path\xxx.py
    a = 9
    # And:
    >>> import xxx
    >>> xxx.a
    9
    # I delete this imported module and modified this module:
    del xxx
    # \sys.path\xxx.py
    a = 9
    b = 8
    # And re-import:
    >>> import xxx
    >>> xxx.b
    # But:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: type object 'xxx' has no attribute 'b'
  2. pmp-p commented on Sep 12, 2020

    pmp-pmannequin
    Mannequin

    Hi, you just need to call del sys.modules['xxx'] and have a look at https://docs.python.org/3.7/library/importlib.html#importlib.invalidate_caches

    before importing again

  3. stevendaprano commented on Sep 12, 2020

    @stevendaprano
    Member

    This is not a bug, it is working as designed. Importing takes the module from the cache. You can either delete the module from the cache:

    del sys.modules['modulename']
    import modulename  # reloads from the module file
    

    or possibly simpler, use reload:

        importlib.reload(modulename)
  4. added
    type-bugAn unexpected behavior, bug, or error
    and removed
    performancePerformance or resource usage
    on Sep 12, 2020
  5. 2 remaining items

  6. added
    type-bugAn unexpected behavior, bug, or error
    and removed
    performancePerformance or resource usage
    on Sep 12, 2020
  7. cheny0y0 commented on Sep 13, 2020

    cheny0y0mannequin
    MannequinAuthor
    --REOPEN--
    >>> # I try:
    >>> import xxx
    >>> del sys.modules['xxx']
    >>> # But:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
        del sys.modules['xxx']
    NameError: name 'sys' is not found
    >>> # I try to import sys.
    >>> import sys
    >>> del sys.modules['xxx']
    >>> import xxx
    >>> xxx.b
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: module 'xxx' has no attribute 'b'
  8. terryjreedy commented on Sep 18, 2020

    @terryjreedy
    Member

    I carefully redid your test, corrected, 3 times, and it works as expected.
    Win 10, 3.9.0rc2.

    >>> import a.tem3 as t
    >>> t.b
    Traceback (most recent call last):
      File "<pyshell#23>", line 1, in <module>
        t.b
    AttributeError: module 'a.tem3' has no attribute 'b'

    # Add 'b = 2' to a/tem3.py and save.

    >>> import sys
    >>> del sys.modules['a.tem3']
    >>> import a.tem3 as t
    >>> t.b
    2

    Without seeing exactly what you did and when, I can only guess that you imported a version of xxx without the 'b' binding. I once added it but *forgot to save* before re-importing. Note that dictionary deletion and importing are well tested operations, so that bugs in normal imports are very unlikely. If you really get a failure, there must be some relevant details that you have omitted, like importing from a remote system that is locally cached, with the local cache unchanged by editing the remote original. Or having an OS bug such that saving a file does not update the 'last changed' time.

    If you disagree with a closing, please request re-opening and give us a chance to reproduce and discuss first rather than doing so yourself.

  9. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.8 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions