34

I understand that when possible one should use

import numpy as np

This helps keep away any conflict due to namespaces. But I have noticed that while the command below works

import numpy.f2py as myf2py

the following does not

import numpy as np
np.f2py #throws no module named f2py

Can someone please explain this?

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
user1318806
  • 785
  • 3
  • 9
  • 13
  • 1
    @roippi have you tried exit your python and enter it and just do `import numpy` then `numpy.f2py` ? It throws an error in my case too – aha Mar 24 '14 at 23:24
  • 2
    Importing a module doesn't import sub-modules. You need to explicitly import the `numpy.f2py` module regardless of whether or not/how `numpy` itself has been imported. – alecbz Mar 24 '14 at 23:39

5 Answers5

22

numpy is the top package name, and doing import numpy doesn't import submodule numpy.f2py.

When you do import numpy it creats a link that points to numpy, but numpy is not further linked to f2py. The link is established when you do import numpy.f2py

In your above code:

import numpy as np # np is an alias pointing to numpy, but at this point numpy is not linked to numpy.f2py
import numpy.f2py as myf2py # this command makes numpy link to numpy.f2py. myf2py is another alias pointing to numpy.f2py as well

Here is the difference between import numpy.f2py and import numpy.f2py as myf2py:

  • import numpy.f2py
    • put numpy into local symbol table(pointing to numpy), and numpy is linked to numpy.f2py
    • both numpy and numpy.f2py are accessible
  • import numpy.f2py as myf2py
    • put my2py into local symbol table(pointing to numpy.f2py)
    • Its parent numpy is not added into local symbol table. Therefore you can not access numpy directly
aha
  • 4,314
  • 4
  • 23
  • 27
18

The import as syntax was introduced in PEP 221 and is well documented there.

When you import a module via

import numpy

the numpy package is bound to the local variable numpy. The import as syntax simply allows you to bind the import to the local variable name of your choice (usually to avoid name collisions, shorten verbose module names, or standardize access to modules with compatible APIs).

Thus,

import numpy as np

is equivalent to,

import numpy
np = numpy
del numpy

When trying to understand this mechanism, it's worth remembering that import numpy actually means import numpy as numpy.

When importing a submodule, you must refer to the full parent module name, since the importing mechanics happen at a higher level than the local variable scope. i.e.

import numpy as np
import numpy.f2py   # OK
import np.f2py      # ImportError

I also take issue with your assertion that "where possible one should [import numpy as np]". This is done for historical reasons, mostly because people get tired very quickly of prefixing every operation with numpy. It has never prevented a name collision for me (laziness of programmers actually suggests there's a higher probability of causing a collision with np)

Finally, to round out my exposé, here are 2 interesting uses of the import as mechanism that you should be aware of:

1. long subimports

import scipy.ndimage.interpolation as warp
warp.affine_transform(I, ...)

2. compatible APIs

try:
    import pyfftw.interfaces.numpy_fft as fft
except:
    import numpy.fft as fft
# call fft.ifft(If) with fftw or the numpy fallback under a common name
hbristow
  • 1,896
  • 1
  • 12
  • 9
2

This is a language feature. f2py is a subpackage of the module numpy and must be loaded separately.

This feature allows:

  • you to load from numpy only the packages you need, speeding up execution.
  • the developers of f2py to have namespace separation from the developers of another subpackage.

Notice however that import numpy.f2py or its variant import numpy.f2py as myf2py are still loading the parent module numpy.

Said that, when you run

import numpy as np
np.f2py

You receive an AttributeError because f2py is not an attribute of numpy, because the __init__() of the package numpy did not declare in its scope anything about the subpackage f2py.

gg349
  • 21,996
  • 5
  • 54
  • 64
  • when you do `import numpy.f2py as myf2py`, how do you access its parent numpy? it seems `import numpy.f2py` allows you to access its parent `numpy`, but `import numpy.f2py as myf2py` doesn't – aha Mar 25 '14 at 00:00
  • You don't access it because you decided you didn't want to use anything from `numpy`, and you only care of using the subpackage. It is similar to using `from foo import bar`: the name `foo` will not be accessible. See the comment after the first example of the docs, [LINK](http://docs.python.org/2/tutorial/modules.html#more-on-modules) – gg349 Mar 25 '14 at 00:05
1

numpy.f2py is actually a submodule of numpy, and therefore has to be imported separately from numpy. As aha said before:

When you do import numpy it creats a link that points to numpy, but numpy is not further linked to f2py. The link is established when you do import numpy.f2py

when you call the statement import numpy as np, you are shortening the phrase "numpy" to "np" to make your code easier to read. It also helps to avoid namespace issues. (tkinter and ttk are a good example of what can happen when you do have that issue. The UIs look extremely different.)

Community
  • 1
  • 1
Ben Schwabe
  • 1,283
  • 1
  • 21
  • 36
1

Well quite an old post but here are my 2 cents over the explanation provided by others.

numpy (refer git repository) package have various subpackages, f2py is one of them other are as core, ma etc

If you refer the init.py in numpy package it has imports like -

from . import core etc 

but it's not having any import for f2py subpackage. That's the reason that a statement like

import numpy as np
np.f2py

won't work but

import numpy as np
np.core

will work.