Can the script engine tell a script in which folder the script is located?



Hey guys,

I’m trying to allow a script in ImageJ/Fiji to open another text file which is located in the same folder as the script. Therefore, the script would like to know the folder in which it is located in…
I had a look in the script service interface, but this one apparently doesn’t offer a getter for this information. Furthermore, the IJ.getDirectory(“current”) is also just in special cases pointing to the right directory… :wink:

Is there any another way?



Hi Robert,

I have a partial answer, specific to python as the scripting language.

In python, you can use

dirOfPy = os.path.dirname(os.path.abspath(__file__))
print "dirOfPy =", dirOfPy

This will give you the full path name of the directory that contains
the .py file that contains this code snippet. So if you run a script
that calls a module that calls a module that contains this code,
you won’t get the directory of the top-level script, but rather, the
directory of the second module.

I have used this for more or less your use case – opening data
files in the directory of the script (or in a separate data directory
that is in a fixed location relative to the directory of the script).

(I don’t know how or whether this can be done with other scripting
languages, or whether there is any sort of language-neutral way
to do it.)

Thanks, mm


Good day!

For the ImageJ macro language please note:

1.51s 5 December 2017
Thanks to Ron DeSpain, the macro function (OpenDialog.getLastDirectory() method) now returns the path to the directory that a macro was loaded from.




Hi @mountain_man,

thanks for the suggestion!

Trying this lead to the error message:

NameError: name '__file__' is not defined

Have you tried this in Fiji? I’m looking for the Fiji way of doing this :wink:

Hey @Herbie,

thanks for your hint!

I’m afraid but this won’t work as well. Just imagine, the user opens the script, then opens some images and does other stuff. After some time, the function will no longer contain the folder of the script.

Thanks anyway guys!



Hello Robert,

Sorry for the misinformation.

You are absolutely right. This doesn’t work (for me, either).

Try this:


import os
import inspect
print inspect.getsourcefile(lambda:0)
dirOfPy = os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0)))
print "dirOfPy =", dirOfPy

When I run this in Fiji (ImageJ 1.51v on ubuntu)

File > New > Script...
File > Open...
browse to /home/user/ and open

I get the output:

Started at Mon Mar 12 10:14:11 EDT 2018
dirOfPy = /home/user

I found this version, with some words of explanation, on
How do I get the path of the current executed file in Python?

Hopefully this will work for you, as well.

(Note, if you type this code into the script editor – so there is no
file – rather than opening a file,


will resolve to None, so the call to abspath will error out.)

I could have sworn that I had run my original suggestion in
Fiji (but maybe I did, and maybe I didn’t). I did, somehow,
in the past, get the location of a script in Fiji, but maybe it
wasn’t with python.

Anyway, I wrote my original post from memory, rather than
verifying as I posted. (I know, bad idea.) I don’t know what
happened here – maybe I had tweaked my environment
somehow, or maybe I was mixing up scripting languages.

Sorry about the confusion.

Thanks, mm


Hey @mountain_man,

incredible! This is the solution!

import os
import inspect
print inspect.getsourcefile(lambda:0)
dirOfPy = os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0)))
print "dirOfPy =", dirOfPy


Started at Mon Mar 12 15:29:00 CET 2018
dirOfPy = /home/rhaase/code/scripts

Thanks a lot! You are my hero!




If you use the “Run” command when you launch your macro (.ijm), you can use the following command to get the path of the last run macro: