Style question: Importing modules from packages
Wolfgang Maier
wolfgang.maier at biologie.uni-freiburg.de
Thu Dec 4 17:10:13 EST 2014
More information about the Python-list mailing list
Thu Dec 4 17:10:13 EST 2014
- Previous message (by thread): Style question: Importing modules from packages - 'from' vs 'as'
- Next message (by thread): Style question: Importing modules from packages - 'from' vs 'as'
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 04.12.2014 22:30, Chris Angelico wrote: > On Fri, Dec 5, 2014 at 7:56 AM, Wolfgang Maier > <wolfgang.maier at biologie.uni-freiburg.de> wrote: >> On 04.12.2014 19:05, Chris Angelico wrote: >>> >>> >>> With os.path it definitely is. With the actual code in question, it's >>> a Python 2.7 project that mostly uses relative imports - inside >>> package.module1 is "import module2" etc - and I was writing an >>> external script that calls on one of the modules. >> >> >> What ? I'm usually thinking Python 3 not 2 and I'm never sure which Python >> 2.x has backported which feature of 3, but I thought implicit relative >> imports like you seem to describe are not working in 2.7 ? > > Hmm, I'm not sure, but certainly it does seem to work that way. Typing > "import foo" from inside a package will import foo.py from the package > directory. I haven't dug into the details of _why_, and if ever the > project shifts to Python 3 (which I would like it to), we might have > to change some of the import lines, but I'd still like to be able to > reference "foo.bar" as meaning the "bar" top-level object in foo.py. > I checked what the docs say about this and it is totally confusing (at least me): https://docs.python.org/3/howto/pyporting.html#from-future-import-absolute-import says: " from __future__ import absolute_import Implicit relative imports (e.g., importing spam.bacon from within spam.eggs with the statement import bacon) do not work in Python 3. This future statement moves away from that and allows the use of explicit relative imports (e.g., from . import bacon). In Python 2.5 you must use the __future__ statement to get to use explicit relative imports and prevent implicit ones. In Python 2.6 explicit relative imports are available without the statement, but you still want the __future__ statement to prevent implicit relative imports. In Python 2.7 the __future__ statement is not needed. In other words, unless you are only supporting Python 2.7 or a version earlier than Python 2.5, use this __future__ statement. " which I read as there has been a stepwise transition between 2.5 and 2.7 so that 2.7 now behaves like Python 3 even without the __future__ statement. OTOH, I believe you, of course, if you're saying implicit relative imports are working just fine in 2.7, but then how to interpret the "In Python 2.7 the __future__ statement is not needed." above ? Wolfgang
- Previous message (by thread): Style question: Importing modules from packages - 'from' vs 'as'
- Next message (by thread): Style question: Importing modules from packages - 'from' vs 'as'
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list