- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 python 如何了解包中的模块有点困惑。例如,python-pptx
包有一个子模块chart
,其目录结构为
tree /usr/local/lib/python2.7/site-packages/pptx/chart
/usr/local/lib/python2.7/site-packages/pptx/chart
├── __init__.py
├── axis.py
├── category.py
├── chart.py
├── data.py
├── datalabel.py
├── legend.py
├── marker.py
├── plot.py
├── point.py
├── series.py
├── xlsx.py
└── xmlwriter.py
现在如果我
import pptx
并将图表
地址设置为
dir(pptx.chart)
那么子模块data
和xlsx
丢失了
['__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__',
'__path__',
'axis',
'category',
'chart',
'datalabel',
'legend',
'marker',
'plot',
'point',
'series',
'xmlwriter']
我可以使用 dir(pptx.chart.axis)
直接寻址 pptx.chart.axis
,但 dir(pptx.chart.data)
> 结果
AttributeError: 'module' object has no attribute 'data'
如果我导入 pptx.chart
,也会发生同样的事情
但我可以毫无问题地导入 pptx.chart.data
。
这里有哪些包导入规则?
Python dir() not displaying all modules in a package
是一个相关的问题,但并没有完全解决我的问题。
正如 Alex Hall 所解释的(此处重新表述),某些包和模块还会加载其他包和模块。例如,使用
python modulefinder.py test.py | grep pptx
我发现,除了许多其他内容之外,还加载了以下内容
P pptx.chart /usr/local/lib/python2.7/site-packages/pptx/chart/__init__.py
m pptx.chart.axis /usr/local/lib/python2.7/site-packages/pptx/chart/axis.py
m pptx.chart.category /usr/local/lib/python2.7/site-packages/pptx/chart/category.py
m pptx.chart.chart /usr/local/lib/python2.7/site-packages/pptx/chart/chart.py
m pptx.chart.datalabel /usr/local/lib/python2.7/site-packages/pptx/chart/datalabel.py
m pptx.chart.legend /usr/local/lib/python2.7/site-packages/pptx/chart/legend.py
m pptx.chart.marker /usr/local/lib/python2.7/site-packages/pptx/chart/marker.py
m pptx.chart.plot /usr/local/lib/python2.7/site-packages/pptx/chart/plot.py
m pptx.chart.point /usr/local/lib/python2.7/site-packages/pptx/chart/point.py
m pptx.chart.series /usr/local/lib/python2.7/site-packages/pptx/chart/series.py
m pptx.chart.xmlwriter /usr/local/lib/python2.7/site-packages/pptx/chart/xmlwriter.py
dir
仅列出已加载的函数,并且由于 pptx
未加载 pptx.data
,因此 dir
也不会加载列出它。要查找所有可能可加载的模块,必须使用 help
help(pptx.chart)
Help on package pptx.chart in pptx:
NAME
pptx.chart
FILE
/usr/local/lib/python2.7/site-packages/pptx/chart/__init__.py
PACKAGE CONTENTS
axis
category
chart
data
datalabel
legend
marker
plot
point
series
xlsx
xmlwriter
并且,如果需要包中的子模块,则应该显式加载它,而不是依赖于它已被其他东西加载的假设。这就是我一直在寻找的规则。
最佳答案
当模块被导入时,无论从哪里导入,它们都会作为属性添加到父模块中。如果它们不是从任何地方进口的,则它们将不可用。这是因为创建模块是一个昂贵的过程,您只想创建实际需要的模块。
在某个地方(可能不是由您直接导入,而是由您直接或间接导入的模块之一)导入了 pptx.chart.axis
。
如果你想使用一个模块,导入它以保证它在那里。如果您不显式导入它,则在其他地方导入它时它可能仍然存在,但它不可靠。
这是 Django 的实际效果:
>>> import sys
>>> [x for x in sys.modules if x.startswith('django')]
[]
>>> import django
>>> [x for x in sys.modules if x.startswith('django')]
['django.utils.version', 'django', 'django.utils.lru_cache', 'django.utils']
>>> django
<module 'django' from '/Users/alexhall/.pyenv/versions/3.5.1/lib/python3.5/site-packages/django/__init__.py'>
>>> django.utils
<module 'django.utils' from '/Users/alexhall/.pyenv/versions/3.5.1/lib/python3.5/site-packages/django/utils/__init__.py'>
>>> django.core
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: module 'django' has no attribute 'core'
>>> django.db
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: module 'django' has no attribute 'db'
>>> import django.db
>>> [x for x in sys.modules if x.startswith('django')]
['django.dispatch', 'django.dispatch.dispatcher', 'django.utils.six.moves', 'django.utils.encoding', 'django.utils.version', 'django.utils.six.moves.urllib', 'django.utils.module_loading', 'django.db', 'django.utils.six', 'django.db.utils', 'django.core.signals', 'django', 'django.utils.functional', 'django.core', 'django.utils.deprecation', 'django.utils.six.moves.urllib.parse', 'django.utils.lru_cache', 'django.conf', 'django.utils.inspect', 'django.utils._os', 'django.core.exceptions', 'django.utils', 'django.conf.global_settings']
>>> django.core
<module 'django.core' from '/Users/alexhall/.pyenv/versions/3.5.1/lib/python3.5/site-packages/django/core/__init__.py'>
>>> django.db
<module 'django.db' from '/Users/alexhall/.pyenv/versions/3.5.1/lib/python3.5/site-packages/django/db/__init__.py'>
关于Python包导入: What's the rule for addressing submodules?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44913487/
我有一个子模块,当我执行 git status 时,我得到: modified: (modified content) 我跑: git submodule update 但仍然得到: modi
我有一个子模块,当我执行 git status 时,我得到: modified: (modified content) 我跑: git submodule update 但仍然得到: modi
有什么区别吗 git submodule init && git submodule update --recursive 和 git submodule update --init --recurs
我掌握了 git submodule 的窍门(一厢情愿?),我想出了更具体的问题,这是一个好兆头...... 我试图在 .gitmodules 中找到 super 项目所指的子模块的哪个版本。和 .g
我花了很多时间阅读和尝试 git 子模块。但我真的放弃了。 我的问题: 我正在克隆一个 git 存储库作为子模块。所以,我做 git submodule add -b master url local
我无法推送我对 git 子模块所做的更改。我已经推送了主项目,得到了这个 mainProject$ git status On branch myBranch Changes not staged f
我正在使用 ROS 构建一个项目,因此,catkin_make 用于构建我的 ROS 节点和库。 我面临的问题是:我在一个包(包 A)中使用一个 git 子模块(因此,我有一个分层的包含文件夹结构)并
我在当前存储库中用作子模块的存储库名称是 droid-media-lib,但是放置此子模块的目录名称是 库 所以我的.gitmodules 看起来像这样 [submodule "droid-media
标题可能听起来有点含糊,但这里有一个错误的例子。假设我们有三个模块 ModuleA、ModuleB 和 ModuleC,因此 ModuleA 依赖于 ModuleB 和 ModuleB 依赖于 Mod
我有一个包含嵌套子模块的 git 仓库。下面两个命令有什么区别? git submodule update --init --recursive git submodule foreach --rec
我有一个 dotfiles 存储库,其中我所有的 vim 插件都存储为子模块,因此当它们有更改时很容易更新。我以为这两个命令做同样的事情,但我注意到这一定不是这种情况。 我知道我在几个子模块中有更新要
当我执行 git submodule update --init 时出现错误 fatal: Needed a single revision Unable to find current revisi
我正在使用 git submodule 在项目之间构建和共享组件。该项目尚未投入生产,因此,此时子模块运行良好。 但是我担心维护和部署,将其转换为 npm 包是个好主意吗? 最佳答案 一个 npm 包
typing 模块将两个类 io 和 re 导出为“伪子模块”,如下所示。通过赋予它们 __all__ 并将它们添加到 sys.modules 中,使它们看起来像模块的目的是什么? 我理解将它们排除在
在我 git submodule update 之后,它总是检查同一个提交。例如 34561。 我为子模块执行 git checkout master,然后执行 git submodule sync。
我一直对此感到困惑: $ git submodule You need to run this command from the toplevel of the working tree. 为什么我需
我刚 pull https://github.com/kolanos/kohana-universe现在我正在尝试更新所有正在运行的模块: git submodule update --init --
我觉得使用 Git 子模块对我的开发工作流程来说有点麻烦。我听说过 Git 子树和 Gitslave。 是否有更多适用于多个存储库项目的工具以及它们如何比较? 这些工具可以在 Windows 上运行吗
我正在尝试开发具有不同“子模块”的 API。例如,有一个称为“todo”,另一个称为“newsletter”。 我想要做的是,我希望能够在开发过程中运行模块本身,并且我希望能够在服务器上运行整个 AP
我正在尝试将子模块支持添加到 project of mine ,但是我似乎无法使用 JGit 来读取特定的提交或记录,而命令行 git 可以很好地做到这一点,尽 pipe 模块所在的头部是分离的(就子
我是一名优秀的程序员,十分优秀!