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?
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