简体   繁体   English

`import ...` 在 Python 3 中是什么意思?

[英]What does `import ...` mean in Python 3?

From a quick search, I know that one dot means in the current directory, two dots mean in the parent directory, and three dots mean in the grandparent directory, but these examples are usually written as from .. import PackageName通过快速搜索,我知道一个点表示在当前目录中,两个点表示在父目录中,三个点表示在祖父目录中,但这些示例通常写为from .. import PackageName

What if the code was just import ... ?如果代码只是import ...呢? Does this import every file in the grandparent directory?这是否会导入祖父目录中的每个文件? (The reason why I ask is that I'm working with some files that has this import statement at the top, but there is nothing in the grandparent directory to import). (我问的原因是我正在处理一些在顶部包含此导入语句的文件,但祖父目录中没有要导入的内容)。

The dots mimic the Unix file system that .这些点模仿 Unix 文件系统, . refers to the current directory and .. refers to the parent.指的是当前目录, ..指的是父目录。 ... has no such conventional meaning. ...没有这样的传统含义。 In Python, however, ... is the literal for the singleton value of the ellipsis class:然而,在 Python 中, ...ellipsis类的单例值的文字:

>>> type(...)
<class 'ellipsis'>

As such, import ... would be a syntax error, as ... is not a valid module name.因此, import ...将是语法错误,因为...不是有效的模块名称。

What if the code was just import ...?如果代码只是导入...怎么办?

as @chepner said, ... means <class 'ellipsis'>, but you can't import it.正如@chepner 所说,... 表示 <class 'ellipsis'>,但您无法导入它。

in PyCharm "import ..." means it is a folded code block:在 PyCharm 中 "import ..." 表示它是一个折叠的代码块:

在此处输入图片说明

the full code is like this(click the “+” button):完整代码是这样的(点击“+”按钮):

在此处输入图片说明

Maybe other IDEs are like this too.也许其他IDE也是这样。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM