``import plone.api`` vs ``from plone import api``

Which one do you prefer:
import plone.api

or:
from plone import api

  • import plone.api
  • from plone import api

0 voters

I usually prefer import plone.api and in general I try to avoid from X import Y...personal taste and a highly opionated issue.

1 Like

Me too.
An API can be for anything, but plone.api.user.get() makes it very clear what is meant.
Also It's easier to do a grep -Ri plone.api.
But if it was a longer import I wouldn't like the import plone.api form.

Plus I agree, this is highly opinionated.

I use from ... import ... because I like shortening code as much as possible.

5 Likes

@fulv good point

In reality, we all use likely both approaches. Perhaps rule of thumb: if you need only a few symbols from a particular import, you use "from x import y"...when you need a lot of symbols (e.g. plone.api, zope.schema) then you use "import x"..but what few and lot means...personal preferences and personal attitudes..

Same here. As i am following the the PEP8 guidelines with a line length of 79 characters its quite useful to shorten the code even if its only one word.

What's the context here? Pycharm has always had trouble with plone.api for me. The least problematic is e.g. from plone.api import portal. But I see those as Pycharm issues not plone.api namespace issues. If it weren't buggy I'd prefer "import plone.api" to prevent any namespace issues with other api libraries.

Plone Foundation Code of Conduct