简体   繁体   English

在 python 中,“import foo.bar as bar”和“from foo import bar”有什么区别?

[英]In python, what is the difference between 'import foo.bar as bar' and 'from foo import bar'?

I see this convention in pyTorch and matplotlib:我在 pyTorch 和 matplotlib 中看到了这个约定:

import torch.nn as nn
import torch.optim as optim

import matplotlib.pyplot as plt

Is there a reason why the whole path ( module.submodule ) is being imported as an alias instead of just the submodule?是否有理由将整个路径( module.submodule )作为别名而不是子模块导入? What is the difference if I import like this:如果我这样导入有什么区别:

from torch import nn
from torch import optim

from matplotlib import pyplot as plt

Edit : So for the generic case:编辑:所以对于一般情况:

import foo.bar as bar    # [1]
from foo import bar      # [2]

Can there exist code which refers to bar such that it will run with [1] and not [2] (and vice versa)?是否存在引用bar的代码,以便它将使用[1]而不是[2]运行(反之亦然)? Ie is there functional difference between these two ways to import?即这两种导入方式之间是否存在功能差异?

Behind the scenes, all of the import statements are essentially mapped to the builtin __import__ eg:在幕后,所有的导入语句本质上都映射到内置的__import__例如:

import torch.nn as nn

becomes变成

nn = __import__("torch.nn", globals(), locals(),  [], 0)

similarly:相似地:

from torch import nn

becomes变成

nn = __import__("torch", globals(), locals(), ["nn"], 0)

Subtly different but functionally equivalent.略有不同但功能相同。

Reference: https://docs.python.org/3/library/functions.html# import参考: https: //docs.python.org/3/library/functions.html#import

import blank as blank lets you reference the module as that name throughout the program. import blank as blank允许您在整个程序中将该模块作为该名称引用。

from blank import blank imports specific methods, functions, and classes from a module. from blank import blank从模块中导入特定的方法、函数和类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 foo.bar()和bar(foo)之间的区别? - Difference between foo.bar() and bar(foo)? Python:将foo.bar导入为bar与从foo导入导入 - Python: import foo.bar as bar vs from foo import bar Google foo.bar 挑战问题:无法导入库 - Google foo.bar Challenge Issue: Can't import libraries 在python装饰器中foo = bar(foo)和something = bar(foo)之间有什么区别? - what is difference between foo=bar(foo) and something=bar(foo) in decorator in python? 如何实现“从foo.bar import baz.qux”这样的内容? - How can I achieve something like “from foo.bar import baz.qux”? 如何从python中的方法从foo导入栏执行全局操作? - How to do a global from foo import bar from a method in python? 是否可以在python中执行“ from module.foo import * as bar”? - Is it possible to do “from module.foo import * as bar” in python? 本地导入的`python -m foo.bar`与`python foo / bar.py` - `python -m foo.bar` vs. `python foo/bar.py` for local imports 在Python中“ from bar import foo”之后,“ [foo]”有什么特殊含义吗? (也许特定于python2) - Is there some special meaning to `[foo]` right after `from bar import foo` in python? (perhaps python2 specific) 使用属性装饰器后,python object 有两个非常相似的属性(foo.bar 和 foo._bar)。 那样行吗? - After using property decorator, python object has two very similar attributes (foo.bar and foo._bar). Is that ok?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM