Why is importing from a package invalid when the aliased name of package is used?

自古美人都是妖i 提交于 2019-12-01 12:54:12

The module name is still numpy, even after you import the module as np.

What the import … as … syntax basically does is this:

np = internal_import_module('numpy')

So the np is just the local name that get used to refer to the numpy module. If you look at the module name of np, you can see that it’s still 'numpy':

>>> import numpy as np
>>> np.__name__
'numpy'

Now, the local name of a module is not being used at all when another import statement is being evaluated. So your from numpy import array is basically just this:

array = internal_import_module('numpy').array

Here array is again just a local name for the array member inside of the numpy module. It is however not a member inside of the np module because there simply isn’t a module with that name.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!