imagej / pyimagej

Use ImageJ from Python
https://pyimagej.readthedocs.io/
Other
473 stars 82 forks source link

Improve Jupyter Dynamic Install instructions #317

Closed gselzer closed 4 weeks ago

gselzer commented 1 month ago

This commit makes a few changes:

@elevans can you test this out on some other platforms and make sure this works?

gselzer commented 1 month ago

This might be a different issue, but if I write:

import sys, os
prefix = sys.prefix.replace("\\", "/") # Handle Windows Paths
%mamba install --yes --prefix {prefix} -c conda-forge pyimagej openjdk=11
os.environ['JAVA_HOME'] = os.sep.join(sys.executable.split(os.sep)[:-2] + ['jre'])

and then put in a separate cell:

import imagej
ij = imagej.init()

I get the following exception:

---------------------------------------------------------------------------
JVMNotFoundException                      Traceback (most recent call last)
Cell In[3], line 1
----> 1 ij = imagej.init()

File ~\micromamba\envs\tmp\Lib\site-packages\imagej\__init__.py:1211, in init(ij_dir_or_version_or_endpoint, mode, add_legacy, headless)
   1208     raise EnvironmentError("Sorry, the interactive mode is not available on macOS.")
   1210 if not sj.jvm_started():
-> 1211     success = _create_jvm(ij_dir_or_version_or_endpoint, mode, add_legacy)
   1212     if not success:
   1213         raise RuntimeError("Failed to create a JVM with the requested environment.")

File ~\micromamba\envs\tmp\Lib\site-packages\imagej\__init__.py:1312, in _create_jvm(ij_dir_or_version_or_endpoint, mode, add_legacy)
   1310     sj.config.add_option("-Djava.awt.headless=true")
   1311 try:
-> 1312     if hasattr(sj, "jvm_version") and sj.jvm_version()[0] >= 9:
   1313         # Disable illegal reflection access warnings.
   1314         sj.config.add_option("--add-opens=java.base/java.lang=ALL-UNNAMED")
   1315         sj.config.add_option("--add-opens=java.base/java.util=ALL-UNNAMED")

File ~\micromamba\envs\tmp\Lib\site-packages\scyjava\_jvm.py:68, in jvm_version()
     63     return jvm_version
     65 # JPype was clueless, which means the JVM has probably not started yet.
     66 # Let's look for a java executable, and ask via 'java -version'.
---> 68 default_jvm_path = jpype.getDefaultJVMPath()
     69 if not default_jvm_path:
     70     raise RuntimeError("Cannot glean the default JVM path")

File ~\micromamba\envs\tmp\Lib\site-packages\jpype\_jvmfinder.py:74, in getDefaultJVMPath()
     72 else:
     73     finder = LinuxJVMFinder()
---> 74 return finder.get_jvm_path()

File ~\micromamba\envs\tmp\Lib\site-packages\jpype\_jvmfinder.py:212, in JVMFinder.get_jvm_path(self)
    210 if jvm_notsupport_ext is not None:
    211     raise jvm_notsupport_ext
--> 212 raise JVMNotFoundException("No JVM shared library file ({0}) "
    213                            "found. Try setting up the JAVA_HOME "
    214                            "environment variable properly."
    215                            .format(self._libfile))

JVMNotFoundException: No JVM shared library file (jvm.dll) found. Try setting up the JAVA_HOME environment variable properly.

So there's still barriers to running pyimagej in Jupyter, at least on Windows...

elevans commented 1 month ago

Thanks for improving this! I'll try this out on the mac, linux and windows machines I have here. You're on Windows 11 right?

gselzer commented 1 month ago

Thanks for improving this! I'll try this out on the mac, linux and windows machines I have here. You're on Windows 11 right?

Yes!

ctrueden commented 4 weeks ago

Is there actually a jvm.dll beneath os.sep.join(sys.executable.split(os.sep)[:-2] + ['jre'])? Does this code work outside a Jupyter notebook? The environment activation for openjdk from conda-forge might modify your PATH variable, meaning you might need to modify this variable also on Windows...

elevans commented 4 weeks ago

Thanks @gselzer for improving this and actually getting this to work on all platforms! Looks great to me!