[英]why does python submodule not show up in list from dir(module)?
This is probably a more general question, because I have seen similar behavior with other modules. 这可能是一个更笼统的问题,因为我在其他模块上看到了类似的行为。 But I will show the issue as I am seeing it with module
matplotlib
. 但是,我将在模块
matplotlib
看到该问题,以展示该问题。 I thought that dir(module)
lists all attributes of the specified module. 我认为
dir(module)
列出了指定模块的所有属性。 So I can do the following: 因此,我可以执行以下操作:
import matplotlib
dir(matplotlib)
Among the results listed is 'dateutil', but 'dates' is NOT listed by dir(matplotlib)
. 列出的结果中有'dateutil',但
dir(matplotlib)
列出'dates'。 But then I do this: 但是然后我这样做:
import matplotlib.dates ## why does this work? `dir(matplotlib)` did not list it
dir(matplotlib) ## this now lists both 'dates' and 'dateutil'
Why does dir(matplotlib)
now list 'dates' as one of its attributes? 为什么
dir(matplotlib)
现在将“日期”列为其属性之一?
What's going on here? 这里发生了什么?
I just noticed now, it appears to me that dateutil is not actually part of matplotlib, but is imported by matplotlib: 我现在才注意到,在我看来dateutil实际上不是matplotlib的一部分,而是由matplotlib导入的:
In [11]: matplotlib.dateutil.__file__
Out[11]: '/anaconda3/lib/python3.6/site-packages/dateutil/__init__.py'
In [12]: matplotlib.__file__
Out[12]: '/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py'
In [13]: matplotlib.dates.__file__
Out[13]: '/anaconda3/lib/python3.6/site-packages/matplotlib/dates.py'
Notice in the output above, matplotlib.dateutil.__file__
shows a file that is not even in the matplotlib directory. 注意,在上面的输出中,
matplotlib.dateutil.__file__
显示的文件甚至不在matplotlib目录中。 Furthermore I can see in file '/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py'
that dateutil is imported by matplotlib/__init__.py
. 此外,我在文件
'/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py'
中可以看到dateutil是由matplotlib/__init__.py
导入的。
So when I run dir(module)
is there an easy way to tell which attributes are part of the module itself, and which were imported by the module? 因此,当我运行
dir(module)
,有一种简单的方法来判断哪些属性是模块本身的一部分,哪些是由模块导入的? And how could I have know (other than reading documentation, or "ls"ing the matplotlib directory) that 'dates' is also a valid "attribute" and "module" within matplotlib? 并且我怎么知道(除了阅读文档,或“ ls” matplotlib目录之外),“ dates”也是matplotlib中有效的“属性”和“模块”?
Simply matplotlib
does not itself import .dates
. 只是
matplotlib
本身并不import .dates
。 What dir()
shows you is a list of attributes of an object. dir()
向您显示的是对象的属性列表。 If matplotlib
does not import .dates
, then there's no local name dates
on the matplotlib
module object. 如果
matplotlib
不import .dates
,则matplotlib
模块对象上没有本地名称dates
。
Now, if you do import matplotlib.dates
, Python's import system resolves that to the file/folder matplotlib/dates.py
or matplotlib/dates/__init__.py
, whichever exists. 现在,如果您确实
import matplotlib.dates
,Python的导入系统matplotlib/dates.py
其解析为文件/文件夹matplotlib/dates.py
或matplotlib/dates/__init__.py
中的任何一个。 It's important to emphasise that matplotlib
itself has nothing to do with that file resolution and hence does not need to "know" in any way about the existence of that submodule. 需要强调的是,
matplotlib
本身与该文件的解析无关,因此不需要以任何方式“知道”该子模块的存在。
If you load a module, it does not automatically load all possible submodules as well. 如果加载模块,它也不会自动加载所有可能的子模块。 That only happens if something explicitly
import
s that name. 只有在明确
import
该名称的情况下,才会发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.