Issues in getting started with pyimagej

Dear *,

I’m just playing with pyimagej and have some issues in getting started. I just installed it with these commands:

conda config --add channels conda-forge 
conda install pyimagej openjdk=8

I then tried to run this program (copied from here):

import imagej
ij = imagej.init('sc.fiji:fiji:2.0.0-pre-10')
ij.getVersion()

I got the following error message. I’m also new to python and that’s why I’m struggling in dealing with the error message. It complains a file is not found, but which file?

C:\Users\rober\Anaconda3\python.exe C:/structure/code/pyplay/pyimagej_test.py
Traceback (most recent call last):
  File "C:/structure/code/pyplay/pyimagej_test.py", line 2, in <module>
    ij = imagej.init('sc.fiji:fiji:2.0.0-pre-10')
  File "C:\Users\rober\Anaconda3\lib\site-packages\imagej\imagej.py", line 104, in init
    import imglyb
  File "C:\Users\rober\Anaconda3\lib\site-packages\imglyb\__init__.py", line 41, in <module>
    config, _ = _init_jvm_options()
  File "C:\Users\rober\Anaconda3\lib\site-packages\imglyb\__init__.py", line 36, in _init_jvm_options
    import scyjava
  File "C:\Users\rober\Anaconda3\lib\site-packages\scyjava\__init__.py", line 122, in <module>
    jnius = _init_jvm()
  File "C:\Users\rober\Anaconda3\lib\site-packages\scyjava\__init__.py", line 64, in _init_jvm
    mvn = str(subprocess.check_output(['mvn', '-v']))
  File "C:\Users\rober\Anaconda3\lib\subprocess.py", line 395, in check_output
    **kwargs).stdout
  File "C:\Users\rober\Anaconda3\lib\subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\rober\Anaconda3\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\rober\Anaconda3\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

I’m running Python 3.7 from Pycharm installed via Anaconda on Windows 10 x64.

Any hint is appreciated!

Cheers,
Robert

Hi Robert,

it appears that the code is trying to run maven and cannot find maven.
If I look in the sources it appears it is trying to run maven to find Java.
It appears you may be able to fix this by setting the environment variable JAVA_HOME.
Why conda doesn’t do this in your environment when you install OpenJDK with conda … I don’t know.

source code line

Edit to add:

It assumes that maven is present as jgo appears to be a requirement for pyimagej.

# attempt to find Java by interrogating maven
# (which we have because it is needed by jgo)
1 Like

Hi @VolkerH ,

thanks for your hints!

  • I tried setting with JAVA_HOME to my JDK folder (as I don’t know where Anaconda installed the OpenJDK)… Didn’t work, same error as before…

  • I also downloaded maven and added it to my PATH variable. Running pyimagej from Pycharm still causes the same error. When running it from the anaconda console, it hangs here for ages (code same as above):

I would really like to play with this emerging technology. However, am I the first who tries it on Windows? If yes, is Windows support on the roadmap?

Thanks a lot again!

Cheers,
Robert

Update, after a while, the console crashes with this error message:

(base) c:\structure\code\pyplay>python pyimagej_test.py
log4j:WARN No appenders could be found for logger (org.bushe.swing.event.EventService).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
May 31, 2019 8:18:53 AM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
08:18:54.368 [SciJava-733b5dbb-Thread-0] DEBUG loci.formats.ClassList - Could not find loci.formats.in.SlideBook6Reader
java.lang.ClassNotFoundException: loci.formats.in.SlideBook6Reader
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_192]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_192]
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_192]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_192]
        at java.lang.Class.forName0(Native Method) [na:1.8.0_192]
        at java.lang.Class.forName(Class.java:264) [na:1.8.0_192]
        at loci.formats.ClassList.parseLine(ClassList.java:196) [formats-api-5.8.2.jar:5.8.2]
        at loci.formats.ClassList.parseFile(ClassList.java:258) [formats-api-5.8.2.jar:5.8.2]
        at loci.formats.ClassList.<init>(ClassList.java:138) [formats-api-5.8.2.jar:5.8.2]
        at loci.formats.ClassList.<init>(ClassList.java:122) [formats-api-5.8.2.jar:5.8.2]
        at loci.formats.ImageReader.getDefaultReaderClasses(ImageReader.java:79) [formats-api-5.8.2.jar:5.8.2]
        at io.scif.bf.BioFormatsFormat.cacheReaderClasses(BioFormatsFormat.java:489) [scifio-bf-compat-3.0.0.jar:3.0.0]
        at io.scif.bf.BioFormatsFormat.<init>(BioFormatsFormat.java:138) [scifio-bf-compat-3.0.0.jar:3.0.0]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [na:1.8.0_192]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) [na:1.8.0_192]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [na:1.8.0_192]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) [na:1.8.0_192]
        at java.lang.Class.newInstance(Class.java:442) [na:1.8.0_192]
        at org.scijava.plugin.PluginInfo.createInstance(PluginInfo.java:306) [scijava-common-2.77.0.jar:2.77.0]
        at org.scijava.plugin.DefaultPluginService.createInstance(DefaultPluginService.java:237) [scijava-common-2.77.0.jar:2.77.0]
        at org.scijava.plugin.DefaultPluginService.createInstances(DefaultPluginService.java:226) [scijava-common-2.77.0.jar:2.77.0]
        at org.scijava.plugin.DefaultPluginService.createInstancesOfType(DefaultPluginService.java:217) [scijava-common-2.77.0.jar:2.77.0]
        at io.scif.services.DefaultFormatService.lambda$initialize$0(DefaultFormatService.java:458) [scifio-0.37.3.jar:0.37.3]
        at org.scijava.thread.DefaultThreadService$2.run(DefaultThreadService.java:221) ~[scijava-common-2.77.0.jar:2.77.0]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_192]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_192]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_192]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_192]
        at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_192]
08:18:54.383 [SciJava-733b5dbb-Thread-0] DEBUG loci.formats.ClassList - Could not find loci.formats.in.ScreenReader
java.lang.ClassNotFoundException: loci.formats.in.ScreenReader
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_192]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_192]
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_192]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_192]
        at java.lang.Class.forName0(Native Method) [na:1.8.0_192]
        at java.lang.Class.forName(Class.java:264) [na:1.8.0_192]
        at loci.formats.ClassList.parseLine(ClassList.java:196) [formats-api-5.8.2.jar:5.8.2]
        at loci.formats.ClassList.parseFile(ClassList.java:258) [formats-api-5.8.2.jar:5.8.2]
        at loci.formats.ClassList.<init>(ClassList.java:138) [formats-api-5.8.2.jar:5.8.2]
        at loci.formats.ClassList.<init>(ClassList.java:122) [formats-api-5.8.2.jar:5.8.2]
        at loci.formats.ImageReader.getDefaultReaderClasses(ImageReader.java:79) [formats-api-5.8.2.jar:5.8.2]
        at io.scif.bf.BioFormatsFormat.cacheReaderClasses(BioFormatsFormat.java:489) [scifio-bf-compat-3.0.0.jar:3.0.0]
        at io.scif.bf.BioFormatsFormat.<init>(BioFormatsFormat.java:138) [scifio-bf-compat-3.0.0.jar:3.0.0]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [na:1.8.0_192]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) [na:1.8.0_192]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [na:1.8.0_192]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) [na:1.8.0_192]
        at java.lang.Class.newInstance(Class.java:442) [na:1.8.0_192]
        at org.scijava.plugin.PluginInfo.createInstance(PluginInfo.java:306) [scijava-common-2.77.0.jar:2.77.0]
        at org.scijava.plugin.DefaultPluginService.createInstance(DefaultPluginService.java:237) [scijava-common-2.77.0.jar:2.77.0]
        at org.scijava.plugin.DefaultPluginService.createInstances(DefaultPluginService.java:226) [scijava-common-2.77.0.jar:2.77.0]
        at org.scijava.plugin.DefaultPluginService.createInstancesOfType(DefaultPluginService.java:217) [scijava-common-2.77.0.jar:2.77.0]
        at io.scif.services.DefaultFormatService.lambda$initialize$0(DefaultFormatService.java:458) [scifio-0.37.3.jar:0.37.3]
        at org.scijava.thread.DefaultThreadService$2.run(DefaultThreadService.java:221) ~[scijava-common-2.77.0.jar:2.77.0]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_192]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_192]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_192]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_192]
        at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_192]

By changing the example code to

import imagej
ij = imagej.init('2.0.0-rc-68')
print(ij.getVersion())

It works!

(base) c:\structure\code\pyplay>python pyimagej_test.py
log4j:WARN No appenders could be found for logger (org.bushe.swing.event.EventService).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
May 31, 2019 8:21:51 AM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
2.0.0-rc-68

(base) c:\structure\code\pyplay>

I guess the example code is wrong then. I will create a github issue about it.

Thanks again @VolkerH !

Cheers,
Robert

The message you posted is merely a warning by Bio-Formats. I’ve seen that before when developing a Java plugin that depends on bio-formats. Your ImageJ should continue running nevertheless…

The original sample code is in a Jupyter notebook, where the return value of ij.getVersion() is displayed as the cell output, hence no need for the print statement. If you’re running the code as a script, you need the print if you want to see any output, of course.

@haesleinhuepf It’s possible that your conda environment isn’t actually activated when you run python from your IDE. I haven’t tested pyimagej from within an IDE yet myself. Starting jupyter notebook from the CLI and running in the notebook worked for me, though. Some of the problems with setting JAVA_HOME (and PYJNIUS_JAR) have been discussed here:

@haesleinhuepf to activate the correct environment when calling python from the IDE, you could try an approach similar to how the KNIME Python integration does it, i.e. by creating a .bat file that activates the conda env and then calls python with the original arguments:

https://docs.knime.com/2018-12/python_installation_guide/index.html#anaconda_setup

1 Like

Without the conda environment being activated he wouldn’t be able to import the package in the first place.

Regarding your comment regarding activating the right conda env in the IDE:
Selecting conda environments is quite convenient in VS Code by clicking at the bottom line:
image
and PyCharm has a similar feature.

2 Likes