Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26732

Re: I thought I understood how import worked...

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 <python-python-list@m.gmane.org>
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 <breamoreboy@yahoo.co.uk>
Subject Re: I thought I understood how import worked...
Date Tue, 07 Aug 2012 18:44:49 +0100
References <roy-B56619.09182407082012@news.panix.com>
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 <roy-B56619.09182407082012@news.panix.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3066.1344361439.4697.python-list@python.org> (permalink)
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

Show key headers only | View raw


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', <module 'broken' from '/home/roy/play/import/foo/broken.pyc'>)
> ('foo.broken', <module 'foo.broken' from '/home/roy/play/import/foo/broken.pyc'>)
>
>
> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

I thought I understood how import worked... Roy Smith <roy@panix.com> - 2012-08-07 09:18 -0400
  Re: I thought I understood how import worked... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-07 13:52 +0000
    Re: I thought I understood how import worked... Roy Smith <roy@panix.com> - 2012-08-07 08:25 -0700
      Re: I thought I understood how import worked... Paul Rubin <no.email@nospam.invalid> - 2012-08-07 08:53 -0700
      Re: I thought I understood how import worked... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-07 17:54 +0000
    Re: I thought I understood how import worked... Cameron Simpson <cs@zip.com.au> - 2012-08-08 08:47 +1000
      Re: I thought I understood how import worked... Roy Smith <roy@panix.com> - 2012-08-07 19:05 -0400
      Re: I thought I understood how import worked... Ben Finney <ben+python@benfinney.id.au> - 2012-08-08 14:14 +1000
        Re: I thought I understood how import worked... Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-08 08:40 +0200
        Re: I thought I understood how import worked... Roy Smith <roy@panix.com> - 2012-08-08 09:12 -0400
        Re: I thought I understood how import worked... Cameron Simpson <cs@zip.com.au> - 2012-08-09 15:52 +1000
  Re: I thought I understood how import worked... Ben Finney <ben+python@benfinney.id.au> - 2012-08-07 23:55 +1000
    Re: I thought I understood how import worked... Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-07 16:14 +0200
    Re: I thought I understood how import worked... Roy Smith <roy@panix.com> - 2012-08-07 08:32 -0700
      Re: I thought I understood how import worked... Terry Reedy <tjreedy@udel.edu> - 2012-08-07 13:15 -0400
  Re: I thought I understood how import worked... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-07 15:10 +0100
  Re: I thought I understood how import worked... Terry Reedy <tjreedy@udel.edu> - 2012-08-07 12:49 -0400
  Re: I thought I understood how import worked... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-07 18:44 +0100
  Re: I thought I understood how import worked... Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-08-08 10:47 +0200

csiph-web