Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail 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; 'broken': 0.03; '(python': 0.05; 'django.': 0.05; 'sys': 0.05; 'executed': 0.07; 'file)': 0.07; 'python': 0.09; 'brett': 0.09; 'imported': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'starter': 0.09; 'weird': 0.15; '(empty': 0.16; '*can*': 0.16; '__init__.py': 0.16; 'cached,': 0.16; 'mechanism.': 0.16; 'message- id:@dough.gmane.org': 0.16; "module's": 0.16; 'namespace,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'settings.py': 0.16; 'subject:import': 0.16; 'twice.': 0.16; 'url:whatsnew': 0.16; 'wrote:': 0.17; 'url:dev': 0.17; 'appears': 0.18; 'module': 0.19; 'skip:p 30': 0.20; 'import': 0.21; 'names.': 0.22; "i've": 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'module.': 0.27; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'cat': 0.29; "i'm": 0.29; 'maybe': 0.29; 'code': 0.31; 'gets': 0.32; 'url:python': 0.32; 'file': 0.32; 'from:addr:yahoo.co.uk': 0.32; 'could': 0.32; 'print': 0.32; 'getting': 0.33; "he's": 0.33; 'to:addr:python-list': 0.33; '(with': 0.33; 'so,': 0.35; 'something': 0.35; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'modules': 0.36; 'test': 0.36; 'problems': 0.36; 'possible': 0.37; 'bad': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'some': 0.38; 'url:docs': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'tracking': 0.61; 'first': 0.61; 'kind': 0.61; 'different': 0.63; 'subject:...': 0.63; 'skip:n 10': 0.63; 'our': 0.65; 'talking': 0.66; 'believe': 0.69; 'smith': 0.71; 'around,': 0.84; 'blow': 0.84; 'demonstrates': 0.84; 'original.': 0.84; 'subject:thought': 0.84; 'skip:/ 30': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: I thought I understood how import worked... Date: Tue, 07 Aug 2012 18:44:49 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-146-13-12.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: X-Antivirus: avast! (VPS 120807-0, 07/08/2012), Outbound message X-Antivirus-Status: Clean X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344361439 news.xs4all.nl 6853 [2001:888:2000:d::a6]:36871 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26732 On 07/08/2012 14:18, Roy Smith wrote: > I've been tracking down some weird import problems we've been having with > django. Our settings.py file is getting imported twice. It has some > non-idempotent code in it, and we blow up on the second import. > > I thought modules could not get imported twice. The first time they get > imported, they're cached, and the second import just gets you a reference to the > original. Playing around, however, I see that it's possible to import a module > twice if you refer to it by different names. Here's a small-ish test case which > demonstrates what I'm talking about (python 2.6.5): > > In directory /home/roy/play/import/foo, I've got: > > __init__.py (empty file) > try.py > broken.py > > > $ cat broken.py > print __file__ > > > $ cat try.py > import broken > import foo.broken > > import sys > for m in sys.modules.items(): > if m[0].endswith('broken'): > print m > > > And when I run try.py (with foo as the current directory): > > $ PYTHONPATH=/home/roy/play/import python try.py > /home/roy/play/import/foo/broken.pyc > /home/roy/play/import/foo/broken.pyc > ('broken', ) > ('foo.broken', ) > > > So, it appears that you *can* import a module twice, if you refer to it by > different names! This is surprising. It means that having non-idempotent code > which is executed at import time is a Bad Thing. > > It also means that you could have multiple copies of a module's global > namespace, depending on how your users imported the module. Which is kind of > mind-blowing. > Maybe not directly applicable to what you're saying, but Brett Cannon ought to know something about the import mechanism. I believe he's been working on it on and off for several years. See http://docs.python.org/dev/whatsnew/3.3.html for a starter on the gory details. -- Cheers. Mark Lawrence.