[英]Why does import package.module.function fail in python?
I've tried googling (to little avail) to more clearly understand what different meaning period.我试过谷歌搜索(收效甚微)以更清楚地了解不同的意义时期。 has during an import statement, vs. once a module has already been imported.在 import 语句期间有,而不是一旦模块已经被导入。
For example, these all work:例如,这些都有效:
import numpy
X = numpy.random.standard_normal
from numpy.random import standard_normal
import numpy.random
but this doesn't work:但这不起作用:
import numpy.random.standard_normal
I'm a bit confused as to why this is.我有点困惑这是为什么。 Why is there a difference in what the period.为什么在什么时期会有差异。 does when accessing a module before vs. after an import?在导入之前和之后访问模块时会怎样?
It's because standard_normal
is a method这是因为standard_normal
是一种方法
<built-in method standard_normal of numpy.random.mtrand.RandomState object at 0x0000029D722FBD40>
whenever you do from numpy.random import standard_normal
you are importing the method每当您执行from numpy.random import standard_normal
时,您都在导入该方法
and i don't think you can do this import numpy.random.standard_normal
cause standard_normal
again is a method, this would be possible if standard_normal
would be some module.而且我认为您不能执行此import numpy.random.standard_normal
因为standard_normal
又是一种方法,如果standard_normal
是某个模块,这将是可能的。
Take a look at this, you when I typed dir(standard_normal)
I get the output of those things which are attributes
and when I typed standard_normal
it says <built-in method standard_normal of numpy.random.mtrand.RandomState object at 0x000002509D504740>
cause it simply says it is a method看看这个,当我输入dir(standard_normal)
时,我得到了 output 这些东西是attributes
,当我输入standard_normal
时,它说<built-in method standard_normal of numpy.random.mtrand.RandomState object at 0x000002509D504740>
cause它只是说这是一种方法
Now when I did this import numpy.random.standard_normal
, you are expecting to import the method right?现在,当我执行此import numpy.random.standard_normal
时,您希望导入该方法吗? But what it really does is trying to import a module, Well... there is no such thing as standard_normal
module or standard_normal.py
file.但它真正做的是尝试导入一个模块,好吧......没有standard_normal
模块或standard_normal.py
文件这样的东西。
Take a look at this again.再看看这个。 I imported the random
module and I used the .
我导入了random
模块并使用了.
operator to access the standard_normal function
.运营商访问standard_normal function
。 You can see the sense of it right.你可以看到它的意义。 Cause on the random.py
module it has there a standard_normal
function or method.因为在random.py
模块上它有一个standard_normal
function 或方法。
Sorry I had to use the CMD.抱歉,我不得不使用 CMD。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.