Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How to use from and import in Python3?

I'm confused on using just import vs. using from and import.

For example: from sklearn import datasets should import datasets from the sklearn module. Also, now when I use datasets, I just call datasets.

However, if I use import sklearn then I must call sklearn.datasets to use datasets.

In this case I don't think datasets is a function, I think it's a module that also has a function load_diabetes.

If I use from sklearn import datasetsfollowed by '''diabetes = datasets.load_diabetes()`` the code works.

If I use import sklearn followed by diabetes = sklearn.datasets.load_diabetes() I get an error undefined variable from import: datasets.

What am I not understanding?

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Yeah, datasets is probably a module inside sklearn. I'm wondering about your last statement, though, sklearn.diabetes.load_diabetes(). Shouldn't that middle diabetes be datasets?

Re '''sklearn.diabetes.load_diabetes()''' you're correct, and I edited the original post.

To clarify, say I have a module (e.g. sklearn) that has modules (e.g. datasets with a function load_diabetes) and functions (e.g. someFunction). If I want to use the functions inside the datasets module, I can either do 1

    from sklearn import datasets
    diabetes = datasets.load_diabetes()
    #in this case I can't do anEx = sklearn.someFunction

or 2

    import sklearn.datasets
    diabetes = sklearn.datasets.load_diabetes()
    #in this case I can't do anEx sklearn.someFunction

or 3

    import sklearn
    from sklearn import datasets    #or import sklearn.datasets
    diabetes = datasets.load_diabetes()
    anEx = sklearn.someFunction    #works in this case

Is the above correct?

Is there a good explanation somewhere how this namespace stuff works out?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I don't think #2 is possible and #3 is somewhat redundant.

Any module can, and usually is, a namespace. Say we create a file name math.py that has two methods, add() and multiply().

I can import all of math with

import math

Or I can import just the function(s) I want with

from math import add or from math import add, multiply or, most cleanly

from math import add
from math import multiply

Some will argue that the last example here is redundant but it's definitely easiest if you find yourself suddenly not needing one of those functions. You can also do from math import * but that potentially clobbers things you have in your current namespace (because your file is a namespace too!) so it's not recommended.

OK, so that covers single files. scikit-learn is obviously not a single file.

You can also create a module from a directory, say math/, but you have to have a file in there named __init__.py. This file is generally blank.

Assume the following layout:

math/
    __init__.py
    add.py (has add())
    multiply.py (has multiply())

Now I can do from math.add import add or from math.multiply import multiply. Unless I have some "special" instructions in the __init__.py, I can't do import math and immediately have access to the add.add() function. I could do from math.add import add and then I'd have add() as a function directly.

Basically, namespaces are representations of your file system, plus two special ones locals() and globals() that exist while you're running scripts or in the interpreter. It can be a little confusing but it generally starts to sink in pretty quickly once you start messing with it.