Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python module import - why are components only available when explicitly imported?

Tags:

I have recently installed scikit-image version 0.11.3. I am using python 2.7.10. When I import the entire module I cannot access the io module.

import skimage img = skimage.io.imread(path_) 

Gives error:

AttributeError: 'module' object has no attribute 'io' 

However the following does not error.

from skimage import io img = io.imread(path_) 

Question: Why?

like image 449
jmsinusa Avatar asked Sep 30 '15 23:09

jmsinusa


People also ask

What happens when a Python module is imported?

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.

What is the difference between from import and import in Python?

Use from import when you want to save yourself from typing the module name over and over again. In other words, when referring to a member of the module many times in the code. Use import when you want to use multiple members of the module.

Why is the use of import all statement not recommended in Python?

Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.

When importing a module into a Python program What is the difference between using the import statement and using the From statement?

The import statement allows you to import all the functions from a module into your code. Often, though, you'll only want to import a few functions, or just one. If this is the case, you can use the from statement. This lets you import only the exact functions you are going to be using in your code.


1 Answers

Quick answer: IO is a submodule. Submodules need to be imported from the parent module explicitly.

Long answer: From section 5.4.2 of the python docs:

When a submodule is loaded using any mechanism (e.g. importlib APIs, the import or import-from statements, or built-in import()) a binding is placed in the parent module’s namespace to the submodule object. For example, if package spam has a submodule foo, after importing spam.foo, spam will have an attribute foo which is bound to the submodule. Let’s say you have the following directory structure:

spam/     __init__.py     foo.py     bar.py 

and spam/init.py has the following lines in it:

from .foo import Foo from .bar import Bar 

then executing the following puts a name binding to foo and bar in the spam module:

>>> >>> import spam >>> spam.foo <module 'spam.foo' from '/tmp/imports/spam/foo.py'> >>> spam.bar <module 'spam.bar' from '/tmp/imports/spam/bar.py'> 

Given Python’s familiar name binding rules this might seem surprising, but it’s actually a fundamental feature of the import system. The invariant holding is that if you have sys.modules['spam'] and sys.modules['spam.foo'] (as you would after the above import), the latter must appear as the foo attribute of the former.

like image 139
Shawn Mehan Avatar answered Oct 14 '22 05:10

Shawn Mehan