Jython isssue with if __name__ == '__main__':

Tags: #<Tag:0x00007fd541f7c5b0> #<Tag:0x00007fd541f7c448> #<Tag:0x00007fd541f7c2e0>


Dear Forum,
me and my users are currently facing a big problem with python scripts.
After some updates the if name == ‘main’: part of the script is no more executed.

from ij import IJ

#__name__ == '__main__'

def run_script():
    IJ.log("script executed")
# If a Jython script is run, the variable __name__ contains the string '__main__'.
# If a script is loaded as module, __name__ has a different value.
if __name__ == '__main__':

IJ.log("not in run script function")

this example logs out only “not in run script function” whetere I run it from script editor or put it in the plugins folder and launch from fiji plugins menu.

What happens? Have I to add some update sites that could fix that?

My computer is a:
OX X Version 10.9.5 with Java 8

and Fiji is :
IMageJ 2.0.0-rc-61/1.51n Java 1.8.0_66

Have a nice day,
Emanuele Martini

Script editor error when working with python scripts

This seems to be a new behavior introduced with recent changes with the scijava scripting framework.

If I run:

print __name__

I get:

Started New_.py at Wed Jun 07 10:14:38 CEST 2017

whereas in previous versions, it would print:


So your immediate workaround would be to change your scripts to something like:

if __name__ in ['__builtin__', '__main__']:

Regardless, the __main__ test you mention is a very common pattern with Python programmers, and I think it would be worth discussing if we can get back to the old behavior to avoid breaking a lot of scripts out there…

@hadim, @ctrueden any comments?

Fiji Jython-scripting error after update
Jython-shaded 2.7.0 throws error when importing modules
Script editor error when working with python scripts

yes I know the reasons about the main part, and yes I agree maybe is better to come back to the old behavior.

Thank you @imagejan

have a nice day
Emanuele Martini


I think we have a regression here but I don’t know where it’s coming…

An easy workaround would be to manually rewrite the Bindings object to set the variable __name__ to __main__. Maybe you see a case where it would not be appropriate? And so how to detect such a situation?


I agree this is a bug. The Python documentation states that this idiom is supposed to work, and the Jython documentation agrees. And this particular idiom also appears in the Jython source code 465 times. :wink:

It happened when we transitioned from our own Jython JSR-223 ScriptEngineFactory implementation to the one bundled in Jython since 2.7.x. While that transition fixed some critical bugs, it seems it somehow broke this __name__ behavior.

If you boot up a Jython 2.7.1 interpreter (e.g. via jrun org.python:jython-standalone), and do print __name__, it indeed still says __main__. So there is a discrepancy in how Jython functionality is invoked.

I wish I had more time to dig in to what’s happening under the hood in Jython’s ScriptEngineFactory, but unfortunately I don’t. So for now, I filed scijava/scripting-jython#9 to remember this issue.