how to find the directory of the current python script

Posted by Aryeh Weiss on
URL: http://imagej.273.s1.nabble.com/how-to-find-the-directory-of-the-current-python-script-tp5023449.html

I am running a Python script form the Fiji script editor, an d need to
be able to import another python function that lives in the same directory.

The brute force way is:

sys.path.append(os.path.abspath("/home/amw/Desktop/projects/alerding/summer2020"))
fromĀ  prune_new import prune

but I do not want a script that relies on that absolute path.

So I tried variations of IJ.getDirectory("current") and such, and none
of them produce the correct result.

I can do:

sys.path.append(IJ.getDirectory(""))
fromĀ  prune_new import prune

and select the directory, but that is annoying to do every run.

I found a solution for macros in this forum post:
https://forum.image.sc/t/does-anyone-have-an-elegant-way-to-id-the-folder-a-macro-is-run-from/7924?u=herbie

where Wayne provided File.directory() for macros. I have not found the
API way to do this for a python script.

Is there a way to do it?

Thanks in advance
--aryeh

--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html