Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41810
| From | Fabian von Romberg <fromberg100@hotmail.com> |
|---|---|
| Subject | Re: import in Python3.3 |
| Date | 2013-03-24 20:39 -0500 |
| References | <mailman.3685.1364166787.2939.python-list@python.org> <514f9a0b$0$30001$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3687.1364175588.2939.python-list@python.org> (permalink) |
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 > <module 'collections' from '/usr/local/lib/python3.3/collections/__init__.py'> > py> mypackage.explicit_import.collections > <module 'mypackage.collections' from './mypackage/collections.py'> > py> mypackage.relative_import.collections > <module 'mypackage.collections' from './mypackage/collections.py'> > > > 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. > > >
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
import in Python3.3 Fabian von Romberg <fromberg100@hotmail.com> - 2013-03-24 18:12 -0500
Re: import in Python3.3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-25 00:27 +0000
Re: import in Python3.3 Fabian von Romberg <fromberg100@hotmail.com> - 2013-03-24 20:39 -0500
Re: import in Python3.3 rocky <rocky@gnu.org> - 2013-03-25 20:49 -0700
Re: import in Python3.3 Jerry Hill <malaclypse2@gmail.com> - 2013-03-26 12:33 -0400
Re: import in Python3.3 rocky <rocky@gnu.org> - 2013-03-26 11:41 -0700
Re: import in Python3.3 rocky <rocky@gnu.org> - 2013-03-26 11:41 -0700
Re: import in Python3.3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-26 23:11 +0000
csiph-web