Login  Register

Re: Strange jython scripts behavior

Posted by dscho on May 19, 2014; 12:45pm
URL: http://imagej.273.s1.nabble.com/Running-batch-files-tp5007769p5007786.html

Hi Leon,

please understand that by replying to Kees, you made your completely
unrelated question part of his thread.

On Mon, 19 May 2014, Leon Espinosa wrote:

> [if you put a .py script] in the plugins folder and try to run it from
> the plugins menu (after re-start FIJI), it says :
>
> "Traceback (most recent call last):
>   File "<string>", line 1, in <module>
> NameError: name 'IJ' is not defined"
>
> But if you open the script editor, open the script (or any .py script)
> and run from the script editor , then all the .py scripts present in the
> plugins menu become available.

This is a feature of the Script editor that Albert Cardona wanted a long
time ago: the editor auto-imports ImageJ 1.x' core classes. This is true
*only* for the editor.

Now, in hindsight (which is 20/20), it seems like a bad idea altogether:
it does not work for classes which differ only in their package names
(there are two Resizer classes, one in ij.plugin and one in
ij.plugin.filter, and that is *just* an example how fragile the
auto-import feature is), it is limited to ImageJ 1.x classes, and it
encourages sloppy programming comparable to .* imports in Java.

The fix is easy: just put

        from ij import IJ

into your .py script.

For your interest: the auto-import feature will be deprecated very soon,
therefore it is a good idea to switch your scripts to proper importing.

Ciao,
Johannes

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