Difference between import numpy and import numpy as np

后端 未结 5 608
说谎
说谎 2021-01-31 16:09

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

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 16:54

    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

提交回复
热议问题