14

Is it ok to use a module referencing with more than two dots in a path? Like in this example:

# Project structure:
# sound
#     __init__.py
#     codecs
#         __init__.py
#     echo
#         __init__.py
#         nix
#             __init__.py
#             way1.py
#             way2.py

# way2.py source code
from .way1 import echo_way1
from ...codecs import cool_codec

# Do something with echo_way1 and cool_codec.

UPD: Changed the example. And I know, this will work in a practice. But is it a common method of importing or not?

Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
  • 2
    what would `...` three dots means in that case? – Anand S Kumar Sep 04 '15 at 10:36
  • 3
    Many years ago I attended a workshop in Django. It was recommended to use relative imports as good practice in Django. However one of the guys running the workshop said that we should use no more than two dots and Guido van Rossum would personally chop off the fingers of those who use triple dot notation for relative imports. I always recall this when it comes to triple dot notation so I avoid to use it. However sometimes I'm really tempted in some particular cases. The reason given was a concern over the readability and losing track of the imports. – cezar Jan 17 '18 at 12:07
  • 2
    I found this `A single dot means that the module or package referenced is in the same directory as the current location. Two dots mean that it is in the parent directory of the current location—that is, the directory above. Three dots mean that it is in the grandparent directory, and so on.` at https://realpython.com/absolute-vs-relative-python-imports/ . Haven't tested, not sure how trustworthy this is. – cowlinator Sep 20 '19 at 22:01

2 Answers2

5

update Nov. 24,2020

If you wanna dig deeper in python's relative-import, I strongly recommend you this answer.


Is it ok to use a module referencing with more than two dots in a path?

Yes. You can use multiple dots in relative import path, but it is only feasible when using from xxx import yyy syntax, not import xxx syntax. Moreover, single dot, two dots and three dots mean current directory, parent directory and grandparent directory respectively, and so on.

And I know, this will work in a practice. But is it a common method of importing or not?

It depends. If your project has complex directory structure, using absolute import would be "disgusting". For example,

from sub1.sub2.sub3.sub4.sub5 import yourmethod

. In this case, using relative import will make your code clean and neat. Maybe look like

from ...sub5 import yourmethod
HuihuangZhang
  • 119
  • 1
  • 5
4

From PEP8:

Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured (such as when a directory inside a package ends up on sys.path):

import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example

However, explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose:

from . import sibling
from .sibling import example

Standard library code should avoid complex package layouts and always use absolute imports.

user202729
  • 3,358
  • 3
  • 25
  • 36
go1dshtein
  • 315
  • 1
  • 2
  • 9
    I've read it. But there is nothing said about relative imports with more than two dots. In example [here](http://stackoverflow.com/questions/14767426/relative-imports) and in some third party python packages it looks like tend to use relative imports. – Ivan Velichko Sep 04 '15 at 11:10