2

If I do

import matplotlib as mpl
mpl.colorbar

-> since colorbar is a submodule and not an attribute, I get the error:

'module' object has no attribute 'colorbar'

whereas if I do (add another import before that):

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.colorbar

it works, I get the

module 'matplotlib.colorbar'

How is this possible!? I mean, I see, that colorbar is a valid attribute of plt, but how does it get into the mpl? Why can something before the import in the second line modify the mpl object which is overwritten in my locals() after that?!
Even if this is matplotlib-specific, it seems my general understanding of python needs an update :)

It does not

2
  • Possible duplicate of matplotlib has no attribute 'pyplot' Commented Nov 6, 2017 at 16:57
  • Edited, hopefully to clarify why the linked question about the submodule/attribute-confusion does not explain the magic happening here. Commented Nov 6, 2017 at 17:12

2 Answers 2

1

Consider the following file structure.

enter image description here

We have two packages aaa and bbb. bbb has a submodule, submodule.py. The content of the files:

aaa/__init__.py:

import bbb.submodule
print("package aaa init")

bbb/__init__.py:

print("package bbb init")

bbb/submodule.py:

print("submodule loaded")

If we now run test.py with the following lines

import bbb
print(bbb.submodule)

we get an error

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

because the submodule has not been loaded yet.

However, if we change text.py to import aaa as well, which itself imports the submodule of bbb

import aaa
import bbb
print(bbb.submodule)

The output will be

package bbb init
submodule loaded
package aaa init
<module 'bbb.submodule' from '...\packagestest\bbb\submodule.py'>

So in this case, bbb.submodule will be found correctly, because it has previously been loaded by the aaa package.

Note that the order of imports does not matter here. The following equally works

import bbb
import aaa
print(bbb.submodule)

Now replace aaa with matplotlib.pyplot, bbb with matplotlib and submodule with colorbar - the effect is the same: pyplot will load matplotlib.colorbar, hence it is available as submodule when calling mpl.colorbar. In case pyplot is not imported, mpl.colorbar will not be found.

Sign up to request clarification or add additional context in comments.

10 Comments

aha, thank you. But well ... if I import aaa, and inside it there is an import spam, then spam gets into my namespace? It doesnt normally, does it? Is it some magic with the __init__.py?
also, if I only import plt, then mpl is not in my namespace (and no mpl.colorbar either, of course). Then after that I import mpl, and it gets modified by the import before? Still confusing
No, spam would not go into your namespace. Also you do not modify anything. You still need to import matplotlib to use matplotlib.colorbar; but because pyplot has already imported matplotlib.colorbar, you will be able to use it. (Also note that all of this is not the recommended way of doing things as it is a source for all kinds of errors.)
sorry, I still dont get something :) I mean, I see that importing aaa makes the init of bbb to be executed. This is a good point, but I still dont see the connection. I mean, why does the meaning of import bbb in test.py change, compared to not having imported aaa. In one case the bbb-object in my locals() has submodule in its namespace, in the other not. Somehow, the first import changes the impact of the second import on my locals - doesn't it?! ... Maybe this is rather a question for the chat, since it's too misunderstandy here, I'll try :)
But thank you for your thorough answer with the MWE, this definitely did throw some light on it. But unfortunately, I can't consider it as solved yet and mark it :)
|
0

Answer to similar Question

All in all, importing just matplotlib is not enough. What you also have to do is to import pyplot since its a submodule from matplotlib and not a function (you can't call it).

You see that if you just import matplotlib but not pyplot and then trying to call pyplot from the console. you get the same error message. I agree that it is a little bit counterintuitive, but you have to import pyplot as a part of matplotlib extra in order to allow the calling of functions that are ONLY a part of the submodule and not the main module. In this case, since there is no colorbar in matplotlib but only in pyplot, you have to import both modules.

2 Comments

yes, the question is somehow similar, I'll edit it to make it more distinct
I hope I could clarify the difference, that my amazement is still quite distinct from that question. But indeed I was not aware, that this question is connected, and it helped me a little in this clarification, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.