Get the current working directory in Paraview 5.4.x when running a macro

How to get the current working directory in Paraview 5.4.x inside a running macro? os.getcwd() returns the base installation directory of Paraview.

Some background:
I have been looking around for a while and failed to find a solution to this. We are using py-Macros to automate some stuff. I am currently modernizing a script for a recent Paraview version (5.10.x) but I want to improve some things along the way while keeping it compatible with the version it was originally scripted for: 5.4.1.
The script involves reading a pvsm state from disc that is positioned in the same folder as the macro. This is an arbitrary folder. Currently, we are required to provide this very directory in the header of the script in order to find required files for the run. The usual way of doing os.getcwd() returns the Paraview base installation path instead of the path the script is located at. Is there another way of accessing the script path while running in the Python shell in Paraview?

Thank you.


Hello Mathieu,

this does throw me an error

NameError: name '__file__' is not defined

How do you run your macro exactly ?

I open the GUI, Tools → Python Shell (in more recent versions this is under View → …), navigate to the script folder, select the script and hit run. Then it starts running.

Works fine here with ParaView 5.11.1:

import os
dirname, filename = os.path.split(os.path.abspath(__file__))
  • run ParaView
  • View → Python Shell
  • Run Script →

I am aware that it works in the recent versions, but I really want to preserve the compatibility of the script for this old version, where it does not work like that. I know its way out of sight, but it would be great to find a solution.