Python from ... import OR import ... 至于模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22245711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
from ... import OR import ... as for modules
提问by embert
Should I use
我应该使用
from foo import bar
OR
或者
import foo.bar as bar
when importing a moduleand and there is no need/wish for changing the name(bar)?
当导入模块,并和有改变名字没有必要/愿望(bar)?
Are there any differences? Does it matter?
有什么区别吗?有关系吗?
采纳答案by Martijn Pieters
Assuming that baris a module or package in foo, there is no difference, it doesn't matter. The two statements have exactly the same result:
假设bar是模块或包中的foo,没有区别,没关系。这两个语句的结果完全相同:
>>> import os.path as path
>>> path
<module 'posixpath' from '/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/posixpath.pyc'>
>>> from os import path
>>> path
<module 'posixpath' from '/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/posixpath.pyc'>
If baris not a module or package, the second form will not work; a traceback is thrown instead:
如果bar不是模块或包,则第二种形式将不起作用;相反,会抛出回溯:
>>> import os.walk as walk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named walk
回答by Depado
The only thing I can see for the second option is that you will need as many lines as things you want to import. For example :
对于第二个选项,我唯一能看到的是,您将需要与要导入的内容一样多的行。例如 :
import foo.bar as bar
import foo.tar as tar
import foo.zar as zar
Instead of simply doing :
而不是简单地做:
from foo import bar, tar, zar
回答by A.Zaben
You can use asto rename modules suppose you have two apps that have views and you want to import them
您可以使用as重命名模块,假设您有两个具有视图的应用程序并且您想导入它们
from app1 import views as views1
from app2 import views as views2
if you want multiple import use comma separation
如果你想多次导入使用逗号分隔
>>> from datetime import date as d, time as t
>>> d
<type 'datetime.date'>
>>> t
<type 'datetime.time'>
回答by cdarke
This is a late answer, arising from what is the difference between 'import a.b as b' and 'from a import b' in python
这是一个迟到的答案,源于python中'import ab as b'和'from a import b'之间的区别
This question has been flagged as a duplicate, but there is an important difference between the two mechanisms that has not been addressed by others.
这个问题已被标记为重复,但这两种机制之间存在重要差异,其他人尚未解决。
from foo import barimports anyobject called barfrom namespace foointo the current namespace.
from foo import bar进口任何所谓的对象bar从命名foo到当前的命名空间。
import foo.bar as barimports an importable object (package/module/namespace) called foo.barand gives it the alias bar.
import foo.bar as bar导入一个名为的可导入对象(包/模块/命名空间)foo.bar并给它别名bar。
What's the difference?
有什么不同?
Take a directory (package) called foowhich has an __init__.pycontaining:
取一个名为的目录(包)foo,其中__init__.py包含:
# foo.__init__.py
class myclass:
def __init__(self, var):
self.__var = var
def __str__(self):
return str(self.__var)
bar = myclass(42)
Meanwhile, there is alsoa module in foocalled bar.py.
同时,在调用中还有一个模块。foobar.py
from foo import bar
print(bar)
Gives:
给出:
42
Whereas:
然而:
import foo.bar as bar
print(bar)
Gives:
给出:
<module 'foo.bar' from '/Users//..../foo/bar.py'>
So it can be seen that import foo.bar as baris safer.
所以可以看出这样import foo.bar as bar更安全。

