0

I'm about to mess around with Adaboost in sklearn, but I just wanted to clarify a small difference that I'm having conceptually.

I've read through this stack overflow article. I've also breezed through some other stack articles with weird import errors. However, I'm still a little confused on why one of these snippets of code works, while the other breaks.

Basically, I'm just messing around with different ways to import sklearn.ensemble. I figured because sklearn is a package, we could do this:

import sklearn
clf = sklearn.ensemble.AdaBoostClassifier
AttributeError: 'module' object has no attribute 'ensemble'

The error tells me that ensemble is not an attribute. I guess this makes sense, because ensemble is itself a package.

This snipped of code works though:

import sklearn.ensemble
clf = sklearn.ensemble.AdaBoostClassifier

My question is, what is the distinction? If we import a package, shouldn't it be fine to reference packages within that super-package? Why would this not be allowed? Feel free to just link me documentation, but I wasn't sure about the best place to look.

Thank you!

Community
  • 1
  • 1
jlarks32
  • 931
  • 8
  • 20

1 Answers1

1

When used by itself, the import keyword is always followed by a module, not a package:

import my_module

If the module that you want to import lives within a package, you can use the dot notation to import that module:

import my_package.my_module

The confusion arises because the name of a package can also be used as the name for the __init__.py module within that package. When you import sklearn, you are not importing all the package contents, but only the module sklearn.__init__.py.

Therefore:

import sklearn #imports the contents of sklearn.__init__.py
import sklearn.ensemble # imports the contents of sklearn.ensemble.py

Another way to import things is the construct

from <module> import <attributes>

In this case, similar rules apply: the from keyboard is followed by a module, not a package. So, if

from sklearn import an_awesome_function

then you are importing an_awesome_function defined in the module sklearn.__init__.py, which is part of the package sklearn.

Antonio
  • 355
  • 1
  • 2
  • 13