do we still believe explicit relative imports are bad as PEP 8 claims?
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
On Fri, Feb 18, 2011 at 3:36 PM, Brett Cannon <brett@python.org> wrote:
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
I suspect the portability argument is about cross-Python-version compatibility, rather than across operating systems. Maybe we don't care about that any more since 2.4 and earlier don't exist in the eyes of python-dev.
On the other hand, I've never used them, or stumbled over code that does, so I won't speak to the readability issue. I have an opinion, but no practical experience with explicit relative imports.
-Fred
-- Fred L. Drake, Jr. <fdrake at acm.org> "A storm broke loose in my mind." --Albert Einstein
Le vendredi 18 février 2011 à 12:36 -0800, Brett Cannon a écrit :
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
I personally find it confusing and unreadable, and I much prefer when I don't have to decipher it (especially non-trivial variants such as "from ..foo import bar").
Just my 2 cents
Antoine.
On Fri, Feb 18, 2011 at 3:59 PM, Antoine Pitrou <solipsis@pitrou.net> wrote:
(especially non-trivial variants such as "from ..foo import bar").
Eeewe.
More than one leading "." should be considered a bug.
-Fred
-- Fred L. Drake, Jr. <fdrake at acm.org> "A storm broke loose in my mind." --Albert Einstein
Brett Cannon wrote:
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
Let's put it this way: I think that PEP 8 gets way too much attention in Python land.
It describes one way of doing things, but is not a bible or strict style guide (and even says that).
Regarding relative imports: I think they were only added to be able to port code that uses Python2-style imports (which are relative as first try, then absolute) gradually to code that uses absolute imports.
In all our larger projects we use absolute imports and this has often helped in organizing the code, finding definitions, etc. So far, we've not had any use for relative imports, but I can imagine some uses for e.g. plugin modules and component architectures that can be dropped into existing Python packages. Relative imports can also help porting code when doing package structure changes, e.g. moving top-level modules into a package.
-- Marc-Andre Lemburg
On Fri, Feb 18, 2011 at 10:35 PM, M.-A. Lemburg <mal@egenix.com> wrote:
Brett Cannon wrote:
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
Let's put it this way: I think that PEP 8 gets way too much attention in Python land.
It describes one way of doing things, but is not a bible or strict style guide (and even says that)
Yeah but it exists. And it very useful to have, I'd say.
1 - when you start Python, it gives you a sense of how a "beautiful" Python code should look. 2 - for any new project, I personally recommend strict PEP 8 instead of inventing another convention. 3 - It's documented, widely adopted, and we have existing tools to check for compliancy out there.
Cheers Tarek
-- Tarek Ziadé | http://ziade.org
On Fri, Feb 18, 2011 at 3:36 PM, Brett Cannon <brett@python.org> wrote:
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
I've been living so long with versions of Python that didn't have explicit relative imports, I'd forgotten why I wanted them in in the first place.
My initial reaction was that absolute imports are good enough, but that there are special cases where relative imports are needed and explicit relative imports address that need, so I'm sure we need the feature.
Thinking about it more though, I *like* explicit relative imports because I think they can reduce the burden of reading the code.
When you see:
from . import foo
You know that foo is local to (part of) this project. Of course, if you see:
import thisproject.foo
You know that foo is part of thisproject
, but you also have to remember that
thisproject
is *this* project. :) How useful this is can
certainly be debated.
I think I'd have to use the explicit relative import style in practice for a while before I was sure I liked it. I think I'll make a point of trying it out.
Of course, the explicit relative import style makes moving packages around *somewhat* easier.
I agree with Marc-Andre. We shouldn't worry too much about what PEP 8 says.
Jim
-- Jim Fulton http://www.linkedin.com/in/jimfulton
On Feb 18, 2011, at 12:36 PM, Brett Cannon wrote:
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
I still find relative imports to be a bit jarring and don't like the implied tight coupling of modules. The nest of relative imports in unittest is a good example of something that causes a mental hiccup when I read it and it seems like an anti-pattern.
Raymond
Lib/unittest/__init__.py
from .result import TestResult from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf, skipUnless, expectedFailure) from .suite import BaseTestSuite, TestSuite from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames, findTestCases) from .main import TestProgram, main from .runner import TextTestRunner, TextTestResult from .signals import installHandler, registerResult, removeResult, removeHandler
On Sat, Feb 19, 2011 at 11:30 AM, Raymond Hettinger <raymond.hettinger@gmail.com> wrote:
On Feb 18, 2011, at 12:36 PM, Brett Cannon wrote:
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
I still find relative imports to be a bit jarring and don't like the implied tight coupling of modules. The nest of relative imports in unittest is a good example of something that causes a mental hiccup when I read it and it seems like an anti-pattern.
The particular pattern employed by unittest would be a pseudo-module (i.e. a package that tries to present itself as really just a module) rather than explicit relative imports in general, though.
I'm personally fine with PEP 8 continuing to advocate absolute imports. Explicit relative imports make certain kinds of code easier to write, but they shouldn't be the default choice for a new project.
Cheers, Nick.
-- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
On Feb 18, 2011, at 12:36 PM, Brett Cannon wrote:
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
I agree with others that explicit relative imports should still be discouraged. I've run into problems with them where imports break under some situations. I don't remember the details, but I think it was when running unittests or under -m or something. Yeah, I should file a bug next time I run into it.
-Barry
On Wed, Feb 23, 2011 at 15:13, Barry Warsaw <barry@python.org> wrote:
On Feb 18, 2011, at 12:36 PM, Brett Cannon wrote:
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
I agree with others that explicit relative imports should still be discouraged. I've run into problems with them where imports break under some situations. I don't remember the details, but I think it was when running unittests or under -m or something. Yeah, I should file a bug next time I run into it.
What kind of example are you setting as the FLUFL if you won't even file a bug report?!? Don't make me hold your __future__ statement ransom!
On Feb 23, 2011, at 03:32 PM, Brett Cannon wrote:
What kind of example are you setting as the FLUFL if you won't even file a bug report?!? Don't make me hold your __future__ statement ransom!
If I filed a bug report every time I actually found a bug, I'd never be able to read email! Wait, hmmm...
-Barry
On Thu, Feb 24, 2011 at 9:13 AM, Barry Warsaw <barry@python.org> wrote:
On Feb 18, 2011, at 12:36 PM, Brett Cannon wrote:
It says they are "highly discouraged" because "absolute imports are more portable and usually more readable", but now that people have had a chance to use explicit relative imports, do people still believe this? I mean if we truly believed this then why did we add the syntax? I know I have used it and love it, let alone that I don't buy the portability argument.
I agree with others that explicit relative imports should still be discouraged. I've run into problems with them where imports break under some situations. I don't remember the details, but I think it was when running unittests or under -m or something. Yeah, I should file a bug next time I run into it.
/me points to PEP 366
Relative imports and __main__ modules inside packages did *not* play nicely with each other at all for a while there.However, as far as I am aware, the only time you get in trouble now is when you run scripts inside packages directly (rather than via -m), but that causes trouble for multiple reasons, not just broken relative imports. If there are other cases that still have issues, I'd definitely like to hear about them.
Cheers, Nick.
-- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (9)
-
Antoine Pitrou
-
Barry Warsaw
-
Brett Cannon
-
Fred Drake
-
Jim Fulton
-
M.-A. Lemburg
-
Nick Coghlan
-
Raymond Hettinger
-
Tarek Ziadé