Why can't I import my own jython modules?

Hoi *,

I wrote a small helper class in Jython today. Now I’d like to use it from another Jython script called test.py

from FragmentManager import FragmentManager
muh = FragmentManager()

The file FragmentManager.py lies in the same folder as does test.py. I even put an empty file named

init.py
there to make this folder a package.

Tried lots of things by now, of course also all kinds of other imports like import FragmentManager or from . import FragmentManager etc. etc.

What am I missing here?

Thanks,
Florian

Did you read how to import other .py scripts how to import other .py scripts?
In essence, you have to add the folder containing your .py script to your path:

path.append(getProperty('fiji.dir') + '/bin')

The BAR update site is a nice example of a python library usable from other scripts.

The drawback is that imported python modules get compiled into a $py.class file (see this thread) that will be ignored (i.e. treated as local-only) by the updater. If you update the corresponding .py file, the compiled $py.class file will not be updated unless you trigger recompiling by deleting it.

3 Likes

@imagejan: the compilation issue was my main motivation to create something like a poor-man’s-maven-replacement for my python libraries:

https://github.com/imcf/imcf-toolbox/tree/master/fiji/libs

Essentially, it’s a Makefile launching Jython via the Fiji-shipped Java-JRE to run a dummy Python script importing all my libraries, therefore triggering the creation of the corresponding $py.class files.

In the end, it packs everything into a JAR file, that I’m using from within Fiji to import my modules from. This way I won’t accidentally fiddle around in my Python code, silently bypassing the updater - at the additional cost of having to make a new JAR every time I change the code.

Which is no big deal, given the current size of my modules.

2 Likes

Thanks @ehrenfeu for pointing this out!

Hi @imagejan,

thanks for your help and sorry for not replying earlier.
Indeed I read the page you sent me the link for, but unfortunately I missed the section relevant for my problem… dang! :wink:

Good to know about this now and maybe also useful to have it in the forum for others to find…

Cheers,
Florian

2 Likes