Paraview PVpython 运行 来自主脚本的下标 - 相对路径

Paraview PVpython running subscript from master script - relative path

我有 PVPython 的主脚本,其中 运行 大约有 3-5 个下标。除了参数之外,我总是必须指定下标路径才能 运行 像这样:

'/home/username/Documents/MainFolder/Subscripts/subscript1.py'

有没有办法自动获取此路径,因为主脚本例如位于同一个主文件夹中,如下所示:

'/home/username/Documents/MainFolder/Masters/master1.py'

当我 运行 来自 PVPython like here 的相对路径引用时,我得到一个错误,所以也许在 PVPython 中有另一种方法可以做?

感谢提示

编辑

问题是

import sys
print(sys.path[0])

我打印了

/usr/lib/python38.zip

在您指向的link(https://note.nkmk.me/en/python-script-file-path/)中,有一段Get the absolute path of the running file.

使用

os.path.abspath(__file__)

获取主脚本的绝对路径。然后您可以从相对路径重新创建下标的绝对路径(请参阅 os 文档:https://docs.python.org/3/library/os.path.html