Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'python,': 0.02; 'package,': 0.03; 'explicitly': 0.04; 'line:': 0.07; 'python': 0.09; 'collections': 0.09; 'content:': 0.09; 'files:': 0.09; 'library?': 0.09; 'path.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'library': 0.15; '"from': 0.16; '"import"': 0.16; '+--': 0.16; '__init__.py': 0.16; "can't.": 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:import': 0.16; 'wrote:': 0.17; 'example.': 0.17; 'library,': 0.17; 'module': 0.19; 'import': 0.21; 'explicit': 0.22; 'work.': 0.23; 'statement': 0.23; 'this:': 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:m 30': 0.26; 'library.': 0.27; 'header:X-Complaints-To:1': 0.28; "d'aprano": 0.29; 'implicitly': 0.29; 'steven': 0.29; 'that.': 0.30; 'relative': 0.30; 'file': 0.32; 'launch': 0.32; 'directory,': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'thanks': 0.34; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'modules': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'containing': 0.61; 'first': 0.61; 'here': 0.65; '*top': 0.84; '2013': 0.84; 'received:186': 0.91; 'shadow': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Fabian von Romberg Subject: Re: import in Python3.3 Date: Sun, 24 Mar 2013 20:39:29 -0500 References: <514f9a0b$0$30001$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 186.68.114.214 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: <514f9a0b$0$30001$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 87 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364175588 news.xs4all.nl 6847 [2001:888:2000:d::a6]:46043 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41810 Hi Steven, thanks a lot for the explanation. I will keep in mind not to use names for my modules that can shadow the standard library. Regards, Fabian On 03/24/2013 07:27 PM, Steven D'Aprano wrote: > On Sun, 24 Mar 2013 18:12:49 -0500, Fabian von Romberg wrote: > >> Hi, >> >> I have a package name collections and inside of my package I want to >> import the collections package from the standard library, but there is >> name conflicts. >> >> How do I import explicitly from the standard library? > > You can't. However, you can import explicitly from your package, or > implicitly by using a relative import. > > Starting from Python 2.7, the "import" statement is always absolute. So > the line: > > import collections > > will always find the first *top level* module or package "collections" in > the python search path. See below for an important proviso. > > Inside your package, you can either use an explicit import like this: > > import mypackage.collections as collections > > or use a relative import like this: > > from . import collections > > Here is a concrete example. I create a package containing five files: > > mypackage/ > +-- __init__.py > +-- collections.py > +-- absolute_import.py > +-- explicit_import.py > +-- relative_import.py > > with the following content: > > # absolute_import.py > import collections > > # explicit_import.py > import mypackage.collections as collections > > # relative_import.py > from . import collections > > > The other two files (collections.py and __init__.py) can be blank. Now, > from *outside* the package, I can do this: > > > py> import mypackage.absolute_import > py> import mypackage.explicit_import > py> import mypackage.relative_import > py> > py> mypackage.absolute_import.collections > > py> mypackage.explicit_import.collections > > py> mypackage.relative_import.collections > > > > Of course "from mypackage import absolute_import" etc. will also work. > > > However, beware: if you cd into the package directory, and then launch > Python, the current directory will contain a file "collections.py" which > will shadow the standard library collections.py. So don't do that. > > >