How do you implement "import matplotlib" and "import matplotlib.pyplot"?

I try to understand some packaging Technics you are using in your project.

To work with pyplot, which is part of your package, I have to import it explicite. This does not work.

import matplotlib
matplotlib.pyplot  # Exception

I have to import it explicte

import matplotlib
import matplotlib.pyplot
matplotlib.pyplot  # OK

This is totally OK for me. No problem with that.

I was looking into matplotlib/src to find out how did you “separate” this two components. But I do not understand. :wink:
There is no __init__.py file in your src and that is why I don’t understand how an import could work. In my understanding even import matplotlib should throw an exception because there is not __init__.py.

I am by no means an expert, but when I look at the matplotlib library that’s installed on the machine I’m using at the moment (a MacBook Pro 2020), the matplotlib installation does have an __init()__ file (quite an extensive one, actually). For reference, I’m using Python 3.10.2 and matplotlib 3.5.1.

With respect to pyplot, it is a separate library (not a class or method from a file called matplotlib.py) and that affects how you import it in order to use its features. In fact, there is no python file called matplotlib.py so when you import matplotlib you are importing the entire package. It may be helpful to take a look at how to work with python modules, which does a much better job of explaining it than I could ever do.

1 Like

Ahh src is where the C++ code lives. The python code (and subpackges) live under lib

1 Like

pyplot is a module under the matplotlib module, but it is not imported in the matplotlib __init__ which is why you can’t do

import matplotlib
matplotlib.pyplot
1 Like