简体   繁体   English

为什么import package.module.function在python会失败?

[英]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.

相关问题 为什么此片段与import_module一起失败? - Why does this snippet with import_module fail? Python如何在内部区分“来自模块导入功能”和“来自模块导入功能” - How does Python internally distinguish “from package import module” between “from module import function” 导入模块(或包).function实际导入整个模块/包? - does import module(or package).function actually import the whole module / package? 为什么这个循环导入在 Python 2 中失败,而在 Python 3 中没有? - Why does this circular import fail in Python 2 but not in Python 3? 为什么安装pysam python包失败? - Why does installing pysam python package fail? 当我从目录外部导入此函数时,为什么我的python import语句失败? - Why does my python import statement fail when I import this function from outside the directory? Python 3.6.1 未找到 package 和模块 [导入] - Python 3.6.1 does not find package and module [import] 为什么&#39;tensorflow&#39;模块导入在Spyder中失败而在Jupyter Notebook中失败而在Python提示符下失败? - Why does the 'tensorflow' module import fail in Spyder and not in Jupyter Notebook and not in Python prompt? Package 和 python 中的模块导入 - Package and module import in python 在模块中导入python包 - Import python package in the module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM