Calling functions from another python script in Fiji

fiji
imagej
python

#1

Hello,

I’m new to writing python scripts for Fiji. I was wondering if there was a way I can call a function from another .py file.

I have Hello_.py

def Hello():
	print "Hello"

I want to call Hello() from main.py:

from Hello_ import Hello

def main():
	Hello()

main()

I get the error message:

line 1, in <module>
    import Hello
ImportError: No module named Hello_

I’m running them in the Fiji Script editor and both files are in the same folder.


#2

I’d do:

import sys
sys.path.append("/path/to/folder/")  # the folder containing Hello_.py
from Hello_ import Hello

Hello()

#3

Note as well that, by inspecting the sys.path entries, you can find out which folders are already included. One of them is your Fiji.app/jars/Lib/. If you add subfolders to Lib/, you can access the .py files
and their functions in them like:

from myfolder.Hello_ import Hello