I am building a piece of software and trying to combine my analysis scripts between Spyder 5.4.1 (Python 3.8) and and ImageJ macros. My datasets are a series of .tif stack images, so I have decided to use PyImageJ to access the ImageJ2 gateway interface to load and run my ImageJ macros through Spyder to have the analysis performed in one automated script.
I utilised the installation method via conda/mamba described here https://pyimagej.readthedocs.io/en/latest/Install.html with the Powershell prompt from Anaconda Navigator 2.3.2
When calling conda list
for the pyimagej environment created, it shows the necessary packages were installed correctly. I change the environment to the one generated from the conda/mamba installation, called 'pyimagej' and open Spyder from the Anaconda Navigator.
I can import and connect to the ImageJ2 gateway without issue.
"Connect to the ImageJ2 API"
import imagej
ij = imagej.init()
dataset = ij.io().open('/Desktop/Test/Test-1.tif')
However, when trying to import an image I get the following error.
Exception in comms call get_namespace_view:
File "\Anaconda3\envs\pyimagej\lib\site-packages\spyder_kernels\comms\commbase.py", line 317, in _comm_message
buffer = cloudpickle.loads(msg['buffers'][0],
jpype._core.JVMNotRunning: Java Virtual Machine is not running
Exception in comms call get_var_properties:
File "\Anaconda3\envs\pyimagej\lib\site-packages\spyder_kernels\comms\commbase.py", line 317, in _comm_message
buffer = cloudpickle.loads(msg['buffers'][0],
jpype._core.JVMNotRunning: Java Virtual Machine is not running
Trying to start a new JVM after initialising an ImageJ2 gateway ij = imagej.init()
with the following code tells me a JVM is already running.
"Start JVM"
import jpype
import jpype.imports
jpype.startJVM()
Traceback (most recent call last):
Cell In[7], line 4
jpype.startJVM()
File ~\Anaconda3\envs\pyimagej\lib\site-packages\jpype\_core.py:166 in startJVM
raise OSError('JVM is already started')
OSError: JVM is already started
Why am I getting an error that states the JVM is not running when it has been started with imagej.init()
? I cannot do anything with PyImageJ because of this error.
This is now a reported and known bug in Spyder >5.4.1 and has to do with the updating of the variable explorer (see Issue #20635). A fix has been proposed with a goal for the release of Spyder 6.
The current workaround is to define the data
variable as a private variable by changing the code to it _data = ij.io().open(/'Test.tif')
.
After testing this workaround, it allows PyImageJ to work with images just fine in Spyder.