How do I install pyimagej?

Hi ctrueden

How can I install this module in my computer?
Should I install a pecific IDE?


The easiest way is to use conda:

conda create -n imagej -c conda-forge openjdk=8 pyimagej

There is an issue reminding us to add these installation instructions.

Is it possible to install pyimage without anaconda?

Hi @setuvora! Welcome to the forum! :smile:

You can use Miniconda. :stuck_out_tongue_winking_eye:

But I assume you mean without any form of Conda. Like, with only pip. Right?

The answer is yes, but you have to do more legwork:

  1. Make sure you are using Python 3, not Python 2.
  2. Install Java 8. E.g. on macOS: brew tap AdoptOpenJDK/openjdk; brew cask install adoptopenjdk8
  3. Install Maven. E.g. on macOS: brew install maven
  4. Install pyimagej including its dependencies: pip install Cython pyimagej
  5. Grab the needed pyjnius.jar file: curl -L | tar xjf - share/pyjnius/pyjnius.jar (We plan to make this step unnecessary in the future by deploying it to a Maven repository, but for now, you need to grab it separately.)
  6. Set the PYJNIUS_JAR environment variable to the full path of the extracted pyjnius.jar file: export PYJNIUS_JAR="$(pwd)/share/pyjnius/pyjnius.jar"
  7. If JAVA_HOME is not already set, set it to your installed Java. On macOS, this should work: export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
  8. Now try it out: python -c 'import imagej; ij = imagej.init(); print(ij.getVersion())'

If you are on Linux, most of the above stuff should be similar, but with your Linux flavor’s package manager rather than Homebrew. If you are on Windows, then I have no insight—may the technogods be with you.

If you are on macOS and get a popup dialog error message about installing Java 6, this is a known issue which can be worked around by hacking your Java installation’s Info.plist to include a JNI capability. I hope to fix it upstream in pyjnius, but it’s still an issue as of this writing.

If you are on macOS and get an error “ImportError: Python is not installed as a framework” then use pythonw instead of python. (If you installed Python 3 using conda, you can do conda install -c conda-forge to get a framework build of Python, and hence the pythonw executable.)

May I ask why you need to avoid using conda?


For building on Windows, check out the conda recipe or the imglyb readme:

ant all
python build_ext --inplace -f
python install
:: Set the appropriate environment variables:
SET "PYJNIUS_JAR=path\to\pyjnius\pyjnius.jar"
SET "JAVA_HOME=path\to\jdk"
1 Like

I was trying to run through these steps and at Step 2, I had to run brew tap AdoptOpenJDK/openjdk before running brew install adoptopenjdk8. Just a heads up!

1 Like

Thanks for the tip, @sethb3744. You are right—I updated the instructions. Note that you also need to use brew cask install adoptopenjdk8 rather than brew install adoptopenjdk8, since the AdoptOpenJDK formulas are casks.

Thanks, ctrueden! conda seems to interfere with cell profiler analyst for me, so I am trying to avoid it. I was able to get pyimagej as per your instructions, but I’m having trouble getting it to run.

Here is the error I get after trying imagej.init();

ERROR 2019-05-07 15:46:22,802: Unable to import scyjava: pyjnius JAR not found.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/imagej/", line 104, in init
    import imglyb
  File "/usr/local/lib/python3.7/site-packages/imglyb/", line 41, in <module>
    config, _ = _init_jvm_options()
  File "/usr/local/lib/python3.7/site-packages/imglyb/", line 36, in _init_jvm_options
    import scyjava
  File "/usr/local/lib/python3.7/site-packages/scyjava/", line 124, in <module>
    raise ImportError('Unable to import scyjava dependency jnius.')
ImportError: Unable to import scyjava dependency jnius.

I tried updating Info.plist to include JNI, but this did not help :confused:

This error means your PYJNIUS_JAR environment variable is not set, or is set to a path that does not actually point to that JAR file. Getting this set correctly entails following steps 5 and 6 of my instructions above:

As in: even having conda installed causes CellProfiler Analyst to function improperly? That is very unfortunate. I would recommend reporting that as a bug to the CellProfiler team (CC @bcimini @agoodman), including details of what exactly goes wrong and how to reproduce.

1 Like