How to call files in the same directory in Python?

In Python, files in the same directory can be imported using relative imports.

Assume the following file structure:

- main.py
- module.py

To call module.py in main.py, you can do it in the following way:

from . import module

Here, . represents the current directory, and “module” represents the name of the file to import (excluding the .py extension).

Furthermore, if you need to call main.py in module.py, you can do so as follows:

import main

Please note that this method is only applicable when directly executed in a script, and not when imported as a module in other files.

bannerAds