0

I am trying to better understand python imports when writing a python package. I come from the R world and when writing an R package which depends on functionality from another R package, we only import that functionality. For example

@importFrom stats qnorm

Sometimes if you are loading a lot of functionality from a package, it is better to import the whole package

@import ggplot2

So my question is, is it better practice to load individual functionality

from seaborn import set, set_context

Or to import whole libraries

import seaborn as sns

I feel that this is different from importing modules into your environment as I am planning on shipping a package and don't want to have namespace clashes.

nathaneastwood
  • 3,664
  • 24
  • 41
  • All `import` does is *bind a name* (the loading of the module is done once the first time, loading the whole module). Bind the names you need to make your code workable. – Martijn Pieters Nov 06 '18 at 14:44
  • In general it is considered best practise to use `import ...` or `import ... as ...` precisely to prevent namespace clashes. If you are building a package yourself, **only** import objects that you plan to export in your `__init__.py`. – FHTMitchell Nov 06 '18 at 14:46
  • Also see ['import module' vs. 'from module import function'](//softwareengineering.stackexchange.com/q/187403) – Martijn Pieters Nov 06 '18 at 14:48
  • Thanks all, I think I understand much better now. – nathaneastwood Nov 06 '18 at 15:22

0 Answers0