PYTHONPATH and module names

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jul 2 00:05:35 CEST 2013


On Mon, 01 Jul 2013 14:38:50 -0700, rusi wrote:

> On Tuesday, July 2, 2013 1:24:30 AM UTC+5:30, Tobiah wrote:
>> > Are you familiar with absolute and relative imports:
>> > http://docs.python.org/release/2.5/whatsnew/pep-328.html
>> 
>> Doesn't seem to work:
>> Python 2.7.3 (default, May 10 2012, 13:31:18) [GCC 4.2.4 (Ubuntu
>> 4.2.4-1ubuntu4)] on linux2 Type "help", "copyright", "credits" or
>> "license" for more information.
>>  >>> from __future__ import absolute_import import .format
>>    File "<stdin>", line 1
>>      import .format
>>             ^
>> SyntaxError: invalid syntax
>>  >>>
>>  >>>
> 1. My reading of
> http://www.python.org/dev/peps/pep-0328/ is that this only works for
> from statements not import statements. [See the section called Guido's
> decision]


Correct. This would have to be written as:

from . import format


but note that this only work in a package, not from some arbitrary module 
inside a directory.


 
> 2. The __future__ is not necessary in python 2.7 [Not necessary or not
> allowed I not know :-) ]

Not necessary.

__future__ statements are guaranteed to "work" in all future versions, in 
the sense that once a __future__ feature is added, it will never be 
removed. So Python has had "nested scopes" since version 2.2 (by memory), 
but:

from __future__ import nested_scopes

still is allowed in Python 3.3, even though it has been a no-op since 2.2 
or 2.3.


-- 
Steven



More information about the Python-list mailing list