1

When I do this ...

import numpy as np

... i can use it but ...

import pprint as pp

... can not, as I need to do this ...

from pprint import pprint as pp

and there is also __import__(str(module)) and possibly more hidden in the documentation.

I have had a few reads around, 'import module' or 'from module import' for instance, but the answers are more targeted towards making a choice on which to use. Also, python-how-to-import-other-python-files just gives more insight on pros and cons.

Could someone shed some light on why there is a difference; what goes on behind the scenes when using the different types of import and how they all work?

Ole Aldric
  • 725
  • 6
  • 22
  • 1
    You can do either one in almost all cases. From just means your are importing something within that module. For example ‘from numpy import arcsin’ – Ryan Schaefer Mar 04 '18 at 23:01
  • Possible duplicate of ['import module' or 'from module import'](https://stackoverflow.com/questions/710551/import-module-or-from-module-import) – Alex Mar 04 '18 at 23:02
  • See the answer after the one you linked. – Alex Mar 04 '18 at 23:02
  • Yes, I can see. But it doesn't answer my question on what goes on behind the scenes here. – Ole Aldric Mar 04 '18 at 23:12

3 Answers3

3

The difference is that the pprint module contains a function called pprint. So when you run import pprint as pp You would need to call pp.pprint to reference the function.

Numpy however is exposed at the top level with all it's functions/classes nested within it.

tmcnicol
  • 576
  • 3
  • 14
3

When you say

When I do this ...

import numpy as np

... it is callable

You're wrong. numpy is not callable. It is a module.

>>> import numpy as np
>>> np()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

Functions the module provides are callable:

>>> np.arange(5)
array([0, 1, 2, 3, 4])

Similarly, the pprint module is not callable. It is a module. The pprint.pprint function is callable:

>>> import pprint as pp
>>> pp.pprint([1, 2, 3])
[1, 2, 3]

There is no need to use from with pprint, or to not use from with numpy. from imports just pull specific contents out of a module; for example, from pprint import pprint as pp gives you the pprint.pprint function as pp. You are essentially never required to perform the import one way or the other.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • How does this determine when and where to use `from`? – Levi Lesches Mar 04 '18 at 23:12
  • So the answer would be along the lines of what @Ryan Schaefer said in the initial comment? My intention was not to speak wrongly about the subject, just to understand what goes on behind the different ways of importing. I've changed the question slightly to reflect what you said. – Ole Aldric Mar 04 '18 at 23:15
  • @OleAnders: Ryan's comment is accurate. – user2357112 Mar 04 '18 at 23:23
3

When importing a module, python needs to find it in the file system and assign it to some variable name in your module. Various forms let you assign a different local name ("as something") or reach into the module and assign one of its interior objects to a local name ("from ...").

import numpy                           # imports numpy and names it "numpy"
import numpy as np                     # imports numpy and names it "np"
from pprint import pprint              # imports pprint anonymously, finds an
                                       #   object in pprint called "pprint"
                                       #   and names it "pprint"
from pprint import pprint as pp        # imports pprint anonymously, finds an
                                       #   object in pprint called "pprint"
                                       #   and names it "pp"
tdelaney
  • 73,364
  • 6
  • 83
  • 116