在python中,是否可以从被调用的文件中获取被调用的路径?

In python, Is it possible to get the path of the invoked from the invoked file?

我想从 ./ 调用 config.py,但我希望 config.py 识别它所在的路径 ("./second/config.py")

我想从“./”调用 config.py 并且我希望 config.py 识别和读取 file.json,但我不想发送任何路径作为参数,也不想附加“./秒”到 sys.path。 有没有一段代码可以让config.py识别出它的相对路径,并通过这种方式把这个路径加入到json的文件名中读取?

./
├── main.py
└── second
      ├── config.py
      └── file.json

我在 config.py

上试过了
def test():
 print(pathlib.Path().absolute())
 print(os.getcwd())

但是,returns

./

我期待 return

/second/config.py

这两个os方法可以一起使用

import os
full_path = os.path.dirname(__file__) + os.path.basename(__file__)