Search code examples
pythonparaviewpvpython

Paraview PVpython running subscript from master script - relative path


I've got master script for PVPython which runs about 3-5 subscripts. Apart from aguments I always have to specify the subscript path in order to run it like this:

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

Is there a way to get this path automatically as the master script would be for example in same main folder like this:

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

When I run a relative path reference from PVPython like here I get an error, so maybe there's another way to do in PVPython?

Thanks for the tips

edit

The problem is that with

import sys
print(sys.path[0])

I get printed

/usr/lib/python38.zip

Solution

  • In the link you point to (https://note.nkmk.me/en/python-script-file-path/), there is a section Get the absolute path of the running file.

    Use

    os.path.abspath(__file__)
    

    to get the absolute path of your master script. Then you can recreate the absolute paths of your subscripts from relative ones (see the os doc : https://docs.python.org/3/library/os.path.html)