[openstack-dev] HACKING and new-style relative imports

Kieran Spear kispear at gmail.com
Wed Jul 10 05:13:28 UTC 2013


On 10 July 2013 02:31, Monty Taylor <mordred at inaugust.com> wrote:
> 
> 
> On 07/09/2013 10:55 AM, Joe Gordon wrote:
>> 
>> On Tue, Jul 9, 2013 at 1:28 PM, Kieran Spear <kispear at gmail.com
>> <mailto:kispear at gmail.com>> wrote:
>> 
>>    Hi all,
>> 
>>    There's a review up to make Horizon pass H304 (no relative imports):
>> 
>>    https://review.openstack.org/#/c/35664/
>> 
>>    Old-style relative imports are dangerous, but I can't think of a
>>    good enough reason to forbid new-style imports, e.g.:
>> 
>>    from .views import IndexView
>> 
>>    instead of
>> 
>>    from openstack_dashboard.dashboards.project.images_and_snapshots.views \
>>          import IndexView
>> 
>>    particularly in Horizon where nesting is deep and you otherwise end
>>    up with many imports that won't fit into 80 characters. I think if
>>    we're going to make the change above the benefits would need to
>>    outweigh the effect on readability.
>> 
>>    There's some discussion on the review already, but I'd like to hear
>>    from a more general audience. Thoughts?
>> 
>> 
>> 
>> http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Imports
>> 
>> "Do not use relative names in imports. Even if the module is in the same
>> package, use the full package name. This helps prevent unintentionally
>> importing a package twice."
>> 
> 
> From pep8:
> 
> Relative imports for intra-package imports are highly discouraged.
> Always use the absolute package path for all imports. Even now that PEP
> 328 is fully implemented in Python 2.5, its style of explicit relative
> imports is actively discouraged; absolute imports are more portable and
> usually more readable.

In Horizon I think it's obvious that relative imports are more readable (and type-able) than the alternative. If portable is referring to moving code around, I think each style has portability benefits for both inter- and intra-package module moves, and there's a lot of overlap.

It sounds like this particular paragraph has been updated to account for PEP328 without considering why new-style imports should be discouraged.

There was a thread on python-dev a few years ago about the same issue:

http://mail.python.org/pipermail/python-dev/2010-October/104476.html

On 10/5/2010 2:21 PM, Guido van Rossum wrote:
> On Tue, Oct 5, 2010 at 11:17 AM, Darren Dale<dsdale24 at gmail.com>  wrote:
>> The issue is implementing a PEP with nice support for relative
>> imports, and then documenting that it should never be used.
> 
> Isn't this mostly historical? Until the new relative-import syntax was
> implemented there were various problems with relative imports. The
> short-term solution was to recommend not using them. The long-term
> solution was to implement an unambiguous syntax. Now it is time to
> withdraw the anti-recommendation. Of course, without going overboard
> -- I still find them an acquired taste; but they have their place.

But nothing ever came of the bug:
http://bugs.python.org/issue10031



> 
> Monty



More information about the OpenStack-dev mailing list