0

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

What if the code was just 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).

1
  • 1
    I don't think it allows you to use ... It should throw an error... Commented Jan 13, 2020 at 23:00

3 Answers 3

3

The dots mimic the Unix file system that . 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:

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

As such, import ... would be a syntax error, as ... is not a valid module name.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think ellipsis is relevant to the question. The name in an import statement is never meant as a variable name and the OP apparently knows it already. The question is really about why from ... import PackageName is allowed while import ... isn't.
Yeah, I missed that from ... import <stuff> is, indeed legal.
0

What if the code was just import ...?

as @chepner said, ... means <class 'ellipsis'>, but you can't import it.

in PyCharm "import ..." means it is a folded code block:

enter image description here

the full code is like this(click the “+” button):

enter image description here

Maybe other IDEs are like this too.

Comments

0

The ... states for the relative import from "grandparent" directory. It follows the convention, that '.' is a current directory, '..' is a parent directory and so on.

For example, I have created three directories a, b, c inside each other and following files in them:

~/example % cat run.py
from a.b.c import c_fun


def main():
    c_fun()

if __name__ == '__main__':
    main()
~/example % cat a/__init__.py

~/example % cat a/b/__init__.py
from .lib import fun

~/example % cat a/b/lib.py
def fun():
    print('Hello')

~/example % cat a/b/c/__init__.py
from .src import c_fun

~/example % cat a/b/c/src.py
from ... import b


def c_fun():
    b.fun()

~/example %

So running run.py gives

~/example % python3 run.py
Hello

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.