[Python]#6 Relative Path

Clay Ryu's sound lab·2023년 8월 9일
0

Framework

목록 보기
20/48

Using dot in import for relative path

In Python imports, the dot . is used to denote a relative import. The line from

.chord import Chord

is importing the Chord class or object from the chord module which is at the same level as the current module in the package hierarchy.
A single dot . before the module name indicates that the import is relative to the current package level. Here's a brief explanation of the notation:

. (single dot): Indicates that the module is in the same package level as the current module.
.. (two dots): Indicates that the module is one level up in the package hierarchy.
... (three dots): Indicates that the module is two levels up in the package hierarchy.

Example

my_package/
    __init__.py
    module_a.py
    module_b.py
    sub_package/
        __init__.py
        module_c.py
        
# If you wanted to import module_b.py from module_a.py, 
# you could use a relative import like this inside module_a.py:
from .module_b import some_function
# Likewise, if you wanted to import module_a.py from module_c.py, you could use:
from ..module_a import some_function
profile
chords & code // harmony with structure

0개의 댓글