Difference between import numpy and import numpy as np

I understand that whenever possible, you should use

import numpy as np 

This helps avoid namespace conflicts. But I noticed that although the command below works

 import numpy.f2py as myf2py 

not performed

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

Can someone explain this?

+29
python numpy
source share
5 answers

numpy is the name of the top package, and import numpy does not import the numpy.f2py submodule.

When you do import numpy , it creates a link that points to numpy , but numpy not bound to f2py . The link is set when you do import numpy.f2py

In the 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 in the local character table (pointing to numpy) and numpy is linked to numpy.f2py
    • numpy and numpy.f2py are available
  • import numpy.f2py as myf2py
    • put my2py in the local character table (pointing to numpy.f2py)
    • Its parent numpy is not added to the local character table. Therefore, you cannot directly access numpy.
+18
source share

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

When importing a module through

 import numpy 

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

Thus,

 import numpy as np 

equivalently

 import numpy np = numpy del numpy 

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

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

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

I also disagree with your statement that "where possible, [import numpy as np] should be." This is done for historical reasons, mainly because people get very tired of the prefix of each operation with numpy very quickly. It never stopped me from running into a name (laziness of programmers actually implies a higher chance of colliding with np )

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

1. long subobjects

 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 
+11
source share

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

This feature allows you to:

  • you download from numpy only the packages you need, speeding up execution.
  • f2py developers have a separate namespace from developers of another subpackage.

Note that import numpy.f2py or its import numpy.f2py as myf2py still loads the numpy parent module.

Said at startup

 import numpy as np np.f2py 

You get an AttributeError because f2py not a numpy attribute because __init__() of the numpy package did not declare anything about the f2py in its f2py .

+2
source share

numpy.f2py is actually a numpy submodule and therefore must be imported separately from numpy. As aha said:

When you import numpy, it creates a link pointing to numpy, but numpy is not related to f2py. The link is set when you do import numpy.f2py

when you call the import numpy as np statement, you shorten the phrase "numpy" to "np" to make the code more readable. It also helps to avoid namespace problems. (tkinter and ttk are a good example of what might happen when you have this problem. The user interface looks very different.)

+1
source share

Well, a pretty old post, but here are my 2 cents for the explanation provided by others.

numpy (see git repository) have different subpackages, f2py is one of them, others are like the kernel, ma, etc.

If you reference init .py in a numpy package, it has an import, for example -

 from . import core etc 

but it does not have imports for the f2py subpackage. This is the reason the statement seems to be

 import numpy as np np.f2py 

won't work but

 import numpy as np np.core 

will work.

0
source share

All Articles