.
indicates the same package as you.
Python also allows you to import in relative paths when you import.For example, if you want to import an internally used package.
When you import a relative path, you have to write that path, but when you want to import something in the same package as yourself, you write from.import~
.
You can also write from.tonarinomodule import~
when you want to import the module next to you
Let's look at a specific example.Suppose you have the following directory structure:
myproject/
├-- packageA
│ -- --_init__.py
│ -- -- module1.py
└-- packageB
├-- __init__.py
├-- ├── module2.py
└-- └── module3.py
In addition, _init__.py
in packageB defines ClassX
and ClassY
in module3.py
.
If you want to import ClassX
or ClassY
from module2.py
, you can write:
from.import ClassX
from.module3 import ClassY
Relative import is useful when you want to import modules right next to you.
On the other hand, relative import depends on the directory structure, so it is difficult to manage for larger projects and is not highly recommended.For example, in the above example, you can import from module2.py
to module1.py
using relative import, but it just changes the location of packageA
on the directory and it doesn't work.
© 2025 OneMinuteCode. All rights reserved.