Difference between import numpy and import numpy as np

后端 未结 5 611
说谎
说谎 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:56

    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.

提交回复
热议问题