[SOLVED] Jython: alternative to __file__

Does anyone has a workaround to get the current script path in the jython environment? For some reason __file__ is not available in the jython environment.

An example use case I have would be to dynamically load my yaml configs from within jython, with configuration files relative to the user script.
Eg: I typically store my configuration files in for more complex modules in jsr223/python/personal/configs/*.yaml

I’d like to do something like

conf_folder = os.path.join(os.path.abspath(os.path.dirname(__file__)), ‘confs’, ‘%s.yaml’ % os.path.basename(__file__).replace(’.py’, ‘’))
with open(conf_folder, ‘r’) as f:
conf = yaml.load(f)

Right now I do the workaround where I store some basic stuff in the configuration.py (following the folder structure from the openhab helper libraries). I know it’s just a minor thing, but somehow I feel more comfortable with strictly splitting my library from the setup specific stuff and personally I like having all configuration options in helper files.

thx

I don’t think there is, but you can get the JVM environment variables and one of them is OH_CONF is the unknown part of the path, the remainder will always be /automation/jsr223/python/personal/configs in your case. Give a bit and I’ll find where I put that code (I didn’t end up using it, but I did test it).

1 Like

Thanks. On 2.5-M1 it seems to be OPENHAB_CONF

os.getenv(‘OPENHAB_CONF’)

did the trick :slight_smile:

There we go, beat me to it!