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


Groups > comp.lang.python > #26715

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!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gandalf@shopzeus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'importing': 0.04; 'sys': 0.05; 'definitions.': 0.07; 'executable': 0.07; 'executed': 0.07; 'imported': 0.09; 'imports': 0.09; 'path.': 0.09; 'sub': 0.09; 'skip:p 40': 0.15; '*can*': 0.16; 'finney': 0.16; 'loaded.': 0.16; 'roy': 0.16; 'somewhere.': 0.16; 'subject:import': 0.16; 'sys.modules': 0.16; 'wrote:': 0.17; 'tend': 0.17; 'appears': 0.18; 'module': 0.19; 'skip:p 30': 0.20; 'import': 0.21; 'names.': 0.22; 'absolute': 0.23; 'somebody': 0.23; 'script': 0.24; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; 'module.': 0.27; "doesn't": 0.28; 'statements': 0.29; 'writes:': 0.29; 'case,': 0.29; 'source': 0.29; 'this.': 0.29; 'usually': 0.30; 'function': 0.30; 'problem.': 0.32; 'url:python': 0.32; 'file': 0.32; 'says': 0.33; 'to:addr:python-list': 0.33; 'tutorial': 0.33; 'project': 0.34; 'list': 0.35; 'clear': 0.35; 'ben': 0.35; 'bigger': 0.35; 'exist': 0.35; 'path': 0.35; 'especially': 0.35; 'so,': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'method': 0.36; 'skip:p 20': 0.36; 'best,': 0.37; 'subject:: ': 0.38; 'url:docs': 0.38; 'sure': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'more.': 0.62; 'different': 0.63; 'subject:...': 0.63; 'times': 0.63; 'skip:\xe2 10': 0.66; 'benefit': 0.70; 'smith': 0.71; 'received:204': 0.72; 'dict.': 0.84; 'subject:thought': 0.84; 'url:tutorial': 0.93
Date Tue, 07 Aug 2012 16:14:15 +0200
From Laszlo Nagy <gandalf@shopzeus.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version 1.0
To python-list@python.org
Subject Re: I thought I understood how import worked...
References <roy-B56619.09182407082012@news.panix.com> <87txweiz7f.fsf@benfinney.id.au>
In-Reply-To <87txweiz7f.fsf@benfinney.id.au>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
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.3055.1344350256.4697.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1344350256 news.xs4all.nl 6961 [2001:888:2000:d::a6]:59126
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:26715

Show key headers only | View raw


On 2012-08-07 15:55, Ben Finney wrote:
> Roy Smith <roy@panix.com> writes:
>
>> So, it appears that you *can* import a module twice, if you refer to
>> it by different names! This is surprising.
> The tutorial is misleading on this. It it says plainly:
>
>      A module can contain executable statements as well as function
>      definitions. […] They are executed only the *first* time the module
>      is imported somewhere.
>
>      <URL:http://docs.python.org/tutorial/modules.html>
>
> but it doesn't make clear that a module can exist in the ‘sys.modules’
> list multiple times under different names.
sys.modules is a dict. But yes, there can be multiple "instances" of the 
same module loaded.

What I do with bigger projects is that I always use absolute module 
names. For example, when I develop a project called "project1" that has 
several sub packages, then I always do these kinds of imports:

from project1.package1.subpackage2.submodule3 import  *
from project1.package1.subpackage2 import  submodule3
from project1.package1.subpackage2.submodule3 import  some_class

Even from a source file that is inside project1.package1.subpackage2, I 
tend to import them the same way. This makes sure that every module is 
imported under the same package path.

You just need to make sure that the main project has a unique name 
(which is usually the case) and that it is on your sys path (which is 
usually the case, especially when the script is started in the project's 
directory).

The cost is that you have to type more. The benefit is that you can be 
sure that you are importing the thing that you want to import, and there 
will be no multiple imports for the same module.

Mabye somebody will give method that works even better.

For small projects without sub-packages, it is not a problem.

Best,

    Laszlo

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