- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个 Python 项目,其结构如下:
testapp/
├── __init__.py
├── api
│ ├── __init__.py
│ └── utils.py
└── utils.py
所有模块都是空的,除了 testapp/api/__init__.py
有以下代码:
from testapp import utils
print "a", utils
from testapp.api.utils import x
print "b", utils
和定义x
的testapp/api/utils.py
:
x = 1
现在我从根目录导入 testapp.api
:
$ export PYTHONPATH=$PYTHONPATH:.
$ python -c "import testapp.api"
a <module 'testapp.utils' from 'testapp/utils.pyc'>
b <module 'testapp.api.utils' from 'testapp/api/utils.pyc'>
导入的结果让我吃惊,因为它表明第二个import
语句已经覆盖了utils
。然而文档指出 from statement will not bind a module name :
The from form does not bind the module name: it goes through the list of identifiers, looks each one of them up in the module found in step (1), and binds the name in the local namespace to the object thus found.
确实,当我在终端中使用 from ... import ...
语句时,不会引入任何模块名称:
>>> from os.path import abspath
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
我怀疑这与 Python 有关,在第二个导入语句时,尝试导入引用 testapp.utils
的 testapp.api.utils
和失败,但我不确定。
这里发生了什么?
最佳答案
来自 import system documentation :
When a submodule is loaded using any mechanism (e.g.
importlib
APIs, theimport
orimport-from
statements, or built-in__import__()
) a binding is placed in the parent module’s namespace to the submodule object. For example, if packagespam
has a submodulefoo
, after importingspam.foo
,spam
will have an attributefoo
which is bound to the submodule. Let’s say you have the following directory structure:spam/
__init__.py
foo.py
bar.pyand
spam/__init__.py
has the following lines in it:from .foo import Foo
from .bar import Barthen executing the following puts a name binding to
foo
andbar
in thespam
module:>>> import spam
>>> spam.foo
<module 'spam.foo' from '/tmp/imports/spam/foo.py'>
>>> spam.bar
<module 'spam.bar' from '/tmp/imports/spam/bar.py'>Given Python’s familiar name binding rules this might seem surprising, but it’s actually a fundamental feature of the import system. The invariant holding is that if you have
sys.modules['spam']
andsys.modules['spam.foo']
(as you would after the above import), the latter must appear as thefoo
attribute of the former.
如果您执行 from testapp.api.utils import x
,import 语句不会将 utils
加载到本地命名空间中。但是,导入机制会将 utils
加载到 testapp.api
命名空间中,以使进一步的导入正常工作。碰巧在您的情况下, testapp.api
也是本地命名空间,所以您会感到惊讶。
关于python - 为什么 Python 的 `from` 形式的 import 语句会绑定(bind)模块名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29974455/
我刚刚通过更改 import * as CodeMirror 修复了一个错误简单明了import CodeMirror . 我复制了this code . (从 TypeScript 移植) impo
我调试(在 PyCharm 中)一个脚本。我在断点处停止,然后转到调试控制台窗口,然后从那里调用导入行,如下所示: import my_util1 from my_utils 然后我调用 my_uti
谁能给我解释一下 import 语句是如何工作的? 例如,我在 myapp/app/models 包中有一个类型 User: package models type User struct {
我想导入 Control.App进入一个引用 PrimIO.PrimIO 的模块通过不合格的名称 PrimIO在很多地方。当然,问题在于 Control.App还导出一个名为 PrimIO 的定义.我
我应该使用 from foo import bar 或者 import foo.bar as bar 当导入模块 还有无需/希望更改名称 (bar)? 有什么不同吗?有关系吗? 最佳答案 假设 bar
我正在 Windows 上使用 Theano 进行深度学习实验的第一步,我很惊讶仅仅加载库需要多少时间。 这是小测试程序: from time import time t0 = time() impo
在 TypeScript 中,如何在不创建任何别名的情况下从文件“导入 *”? 例如我有一个包含顶级导出函数的文件“utils”,我想导入所有这些函数而不为每个函数重新创建别名。 像这样: impor
我应该使用 from foo import bar 或 import foo.bar as bar 当导入模块并且不需要/希望更改名称(bar)? 有什么不同吗?有关系吗? 最佳答案 假设bar是fo
这个问题在这里已经有了答案: Use 'import module' or 'from module import'? (23 个回答) 关闭8年前。 我想知道代码片段之间是否有任何区别 from u
我试过了 from urllib import request mine = request.Request() 和 import urllib.request mine = urllib.reque
所以,我有一个关于 Python 导入的小谜团。我确信出于某种原因事情应该是这样的,因为 Guido 很少出错。但是,为什么会这样呢? $ cat myModule.py #!/usr/bin/pyt
我们正在将 Rails 3.2 应用程序升级到 Rails 4.0。 我们有一个 assets/stylesheets/application/index.css.sass加载一些其他 sass 文件
我正在开发一个相当小的 Typescript 代码库,该代码库已经足够大,可以拆分到多个文件中。这是一个二十一点游戏。我目前有一堆代码,看起来像: var player = new Player();
是否可以以当模块为 use 时的方式编写模块? d 没有显式导入所有子例程都被导入,当它是 use d 显式导入只有这些显式导入的子程序可用? #!/usr/bin/env perl6 use v6;
这个问题在这里已经有了答案: how to watch changes in whole directory/folder containing many sass files (9 个回答) 5年前
我真的很难让它在 xcode 4 中工作。 我有一个项目将在许多应用程序(网络)中重用,因此我创建一个工作区并添加我的两个项目。到目前为止,一切都很好....这就是失败的地方.. #import "J
经典提取器和新提取器之间的主要区别是什么,哪个最好用? 最佳答案 经典提取器使用原始工作流程,与爬虫和连接器相同。 新的提取器更加精简,通常看起来和感觉都更好,并且经典提取器中的许多小错误已在新提取器
在处理 google webfont import mixin 时,我注意到无法动态构建 @import URL。 .gFontImport (@name, @weights, @subsets) {
我正在关注Django 1.8 tutorial 。在我的项目中mysite ,有一个源文件夹polls 。文件夹中有views.py模块其中 index函数已定义。还有一个urls.py文件: fr
我想使用名为 warp 的第三方库编译一个简单的 Rust 程序: [package] name = "hello-world-warp" version = "0.1.0" [dependencie
我是一名优秀的程序员,十分优秀!