0

I tried the following code to import pprint from pprint module like import matplotlib.pyplot as plt. But I got an error instead. Why did I get this error?

import pprint.pprint as p
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-10-029262df2bb9> in <module>
----> 1 import pprint.pprint as p

ModuleNotFoundError: No module named 'pprint.pprint'; 'pprint' is not a package
Rama
  • 45
  • 6
  • 1
    `pyplot` is a **submodule** of `matplotlib`. Therefore you can use `import matplotlib.pyplot as plt`. `pprint.pprint` is a method, and cannot be imported like that. – matszwecja Aug 24 '22 at 07:43

1 Answers1

4

Because there's no module pprint.pprint, only a module pprint. You mean:

from pprint import pprint as p

(From the module pprint import the name pprint as p.)

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Is there any command to know whether a particular method one is importing is a module or method on the lines of help()? – Rama Aug 24 '22 at 08:25
  • The big clue is that you’re *calling* `pprint()`, not access further attributes on it… – deceze Aug 24 '22 at 08:26