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


Groups > comp.lang.python > #21227

Re: Trying to understand 'import' a bit better

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=4044facd4=jeanmichel@sequans.com>
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; 'package,': 0.03; 'library,': 0.05; 'modules.': 0.07; 'script,': 0.07; 'python': 0.08; 'executed': 0.09; 'executes': 0.09; 'invokes': 0.09; 'recognise': 0.09; 'relies': 0.09; 'top-level': 0.09; 'package.': 0.12; 'startup': 0.15; "subject:' ": 0.15; 'complicated,': 0.16; 'container,': 0.16; 'dangerous,': 0.16; 'exposes': 0.16; 'latter,': 0.16; 'realised': 0.16; 'scenarios': 0.16; 'subject:Trying': 0.16; 'subject:bit': 0.16; 'subject:import': 0.16; 'subtlety': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'insert': 0.19; 'cc:no real name:2**0': 0.21; 'pointed': 0.21; 'header:In-Reply-To:1': 0.22; '(or': 0.22; 'works.': 0.23; 'consist': 0.24; 'library.': 0.24; 'developing': 0.25; 'right.': 0.25; 'guess': 0.26; 'cc:2**0': 0.26; 'module': 0.26; 'import': 0.27; 'not.': 0.28; 'bit': 0.28; 'script': 0.28; 'second': 0.28; 'script.': 0.29; 'cc:addr:python.org': 0.29; 'correct': 0.29; 'imported': 0.30; 'invoke': 0.30; 'modules': 0.32; 'there': 0.33; 'header:User-Agent:1': 0.33; 'it.': 0.33; 'instead': 0.33; 'normally': 0.34; 'certain': 0.34; '...': 0.35; 'project': 0.35; 'two': 0.36; 'realise': 0.37; 'recently,': 0.37; 'but': 0.37; 'similar': 0.37; 'using': 0.37; 'references': 0.38; 'some': 0.38; 'think': 0.38; 'except': 0.39; 'being': 0.40; 'user': 0.40; 'change': 0.40; 'more': 0.61; 'kind': 0.62; 'frank': 0.64; 'therefore,': 0.68; 'ages': 0.77; 'programs,': 0.80; 'disastrous': 0.84; 'distinguish': 0.84; 'scenario': 0.93; 'subject:better': 0.93
X-IronPort-AV E=Sophos;i="4.73,533,1325458800"; d="scan'208";a="213171"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Mon, 05 Mar 2012 11:40:26 +0100
From Jean-Michel Pichavant <jeanmichel@sequans.com>
User-Agent Mozilla-Thunderbird 2.0.0.24 (X11/20100328)
MIME-Version 1.0
To Frank Millman <frank@chagford.com>
Subject Re: Trying to understand 'import' a bit better
References <jiv8bt$p2n$1@dough.gmane.org>
In-Reply-To <jiv8bt$p2n$1@dough.gmane.org>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Cc python-list@python.org
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.401.1330944034.3037.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330944034 news.xs4all.nl 6905 [2001:888:2000:d::a6]:38959
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21227

Show key headers only | View raw


Frank Millman wrote:
> Hi all
>
> I have been using 'import' for ages without particularly thinking about it - 
> it just works.
>
> Now I am having to think about it a bit harder, and I realise it is a bit 
> more complicated than I had realised - not *that* complicated, but there are 
> some subtleties.
>
> I don't know the correct terminology, but I want to distinguish between the 
> following two scenarios -
>
> 1. A python 'program', that is self contained, has some kind of startup, 
> invokes certain functionality, and then closes.
>
> 2. A python 'library', that exposes functionality to other python programs, 
> but relies on the other program to invoke its functionality.
>
> The first scenario has the following characteristics -
>   - it can consist of a single script or a number of modules
>   - if the latter, the modules can all be in the same directory, or in one 
> or more sub-directories
>   - if they are in sub-directories, the sub-directory must contain 
> __init__.py, and is referred to as a sub-package
>   - the startup script will normally be in the top directory, and will be 
> executed directly by the user
>
> When python executes a script, it automatically places the directory 
> containing the script into 'sys.path'. Therefore the script can import a 
> top-level module using 'import <module>', and a sub-package module using 
> 'import <sub-package>.<module>'.
>
> The second scenario has similar characteristics, except it will not have a 
> startup script. In order for a python program to make use of the library, it 
> has to import it. In order for python to find it, the directory containing 
> it has to be in sys.path. In order for python to recognise the directory as 
> a valid container, it has to contain __init__.py, and is referred to as a 
> package.
>
> To access a module of the package, the python program must use 'import 
> <package>.<module>' (or 'from <package> import <module>'), and to access a 
> sub-package module it must use 'import <package>.<sub-package>.<module>.
>
> So far so uncontroversial (I hope).
>
> The subtlety arises when the package wants to access its own modules. 
> Instead of using 'import <module>' it must use 'import <package>.<module>'. 
> This is because the directory containing the package is in sys.path, but the 
> package itself is not. It is possible to insert the package directory name 
> into sys.path as well, but as was pointed out recently, this is dangerous, 
> because you can end up with the same module imported twice under different 
> names, with potentially disastrous consequences.
>
> Therefore, as I see it, if you are developing a project using scenario 1 
> above, and then want to change it to scenario 2, you have to go through the 
> entire project and change all import references by prepending the package 
> name.
>
> Have I got this right?
>
> Frank Millman
>
>
>
>   
I think you got it right. It happened to me as well, I guess it's a 
classic for program  being promoted into a library.

JM

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


Thread

Re: Trying to understand 'import' a bit better Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-03-05 11:40 +0100

csiph-web