Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Reason for not allowing import twice but allowing reload() Date: Mon, 29 Feb 2016 00:02:13 -0700 Lines: 32 Message-ID: References: <645cdd46-d4a4-49b3-a0d8-848608d70d73@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de QYDcs6w2GaQqiA7qR6S5EgDfZNd30sWoMX0sazjwzWmw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'cache': 0.05; 'newbie': 0.05; 'sys': 0.05; 'cached': 0.09; 'exception,': 0.09; 'imported': 0.09; 'imports': 0.09; 'subject:()': 0.09; 'python': 0.10; 'subject:not': 0.11; 'importing': 0.15; 'subsequent': 0.15; '2016': 0.16; 'caches': 0.16; 'confusion': 0.16; 'earlier.': 0.16; 'ignores': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reimport': 0.16; 'statement)': 0.16; 'subject:import': 0.16; 'sys.modules': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'module,': 0.18; "shouldn't": 0.18; 'trying': 0.22; 'feb': 0.23; 'import': 0.24; 'header:In- Reply-To:1': 0.24; 'module': 0.25; 'script': 0.25; 'error': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'module.': 0.27; 'does,': 0.29; 'possibly': 0.32; 'though,': 0.32; 'instances': 0.33; 'raised': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'possible.': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'times.': 0.38; 'received:209': 0.38; 'why': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'skip:n 10': 0.62; 'making': 0.62; 'potentially': 0.67; 'dict.': 0.84; 'resulted': 0.84; 'to:name:python': 0.84; 'imagine': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=XpBvjR8NvKfmCNphoT2VU4A+tIZ4NUNQPP2aBR2QgBY=; b=kjJoAs7bjPFQIVmrWWFxYFuwDzL4C/lDnq7FsLtoDozoYRuixwQgQSp2tCyuMPLBHf IpZrcaV+10rattzq6AN6DSwZKAmV8Zo4NjKYJUoizWXoAwnFGDVivfcrMn4mGwYUoJhT RBXXdD7WQmNfOMtHVkrPmlZfF76tjSShVL5eduws+73ZaaAFqduiOj/+63cOzXGtcjh5 rKGYFkgQ1P/qdw/SSLQvWr+QuLvZxAYKrd+oWUE8tmDVZCdnC0gNZlNyzmyvlp6xCjUo lR77YK88vHZxgVd6rPobLB/QE30j7D+AFsDFdZZ2miWja0B5XetMI8J2yOsLi3yVkV6H CRVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=XpBvjR8NvKfmCNphoT2VU4A+tIZ4NUNQPP2aBR2QgBY=; b=CypBVQFSIQltPWjqDLjwsqGNJ1qtmZxcaMYfUXIoAh7ir4qTOajbpnjw3W/iMvpz+4 QHGonxZLr6bsjOuP9SNY1HuSV1ym7F1t/h/ALp0gg6/XWjVrFrJ8U5TqCaUnvUwEueG3 +jb5r4iWlW1sICyItZzrGgip9ZanaBSu7jm8H37bT0I388t9WM3z9UJE8e4vD7vabzEJ IZuwq70lJ0ZtlEIee7LJZ8KxpzLzbBHrR178yOZv2Lp4N7zy5O4YCIhlnICv+T/3iyS4 Agl+lUhgocLsuFmEoZMKpm2crRdBmVO9j8Hm6g96/+FlO9dohJZ0U89w2aBJO0KOu0Av UyZQ== X-Gm-Message-State: AG10YOSQNDc0RjfoxwVc0IyCpX6wqqnhlF+3h56xEQU15JeJK7eIW6yk0MhY66bTTIcn+V4ks9Y1Zh26oRv92Q== X-Received: by 10.107.19.90 with SMTP id b87mr20896993ioj.11.1456729373027; Sun, 28 Feb 2016 23:02:53 -0800 (PST) In-Reply-To: <645cdd46-d4a4-49b3-a0d8-848608d70d73@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103688 On Sun, Feb 28, 2016 at 11:40 PM, wrote: > Hello list, > > We can not import a module twice in a session of Python (subsequent attempts to import same module don't result in any error though, but it is not-effective). > > However after making change to module, we can reload() it (if not reload(), we could possibly have reimport statement) to get the updates in module. > > I am a newbie in Python (learning via IDLE) and trying to understand > > - what is extra (special) that import does, that reload() doesn't? When you import a module, it's cached in the sys.modules dict. Subsequent imports of the same module find the cached module rather than reimport it. However, reload() ignores the cache and always reloads the module. > - if subsequent imports of same module in a session are not effective, why not simply flag those attempts as an error, rather than letting them go effect-less. Because there are legitimate reasons for importing the same module multiple times. For example, I have a script that imports the sys module. It also imports the os module, which imports the sys module. Both the main module of my script and the os module have need of the sys module, so they both import it. If subsequent imports of the module raised an exception, this wouldn't be possible. However, they shouldn't get two separate copies of the sys module, because that would be wasteful and potentially error-prone if the module is stateful In the case of the sys module, there is state -- the sys.modules cache that I mentioned earlier. Imagine the confusion if two separate imports of the sys module resulted in two separate instances with two separate caches of imported modules that were in disagreement.