Cannot find imagej module after installing pyimagej with conda

@ctrueden

I am working on a python project that involves image analysis also and have followed your advice on how to use pyimagej. I use the PyCharm IDE and I started a new project with conda. Then I used the command that you shared:

After doing that the IDE terminal output:

To activate this environment, use
$ conda activate imagej
To deactivate an active environment, use
$ conda deactivate

Now that I have activated imagej by with the provided command I am at a loss of how to use the package. I have the following code in my test file and it throws an error saying that there is no module found with that name.

import imagej
ij = imagej.init()

After activating imagej, how can I use it in my program?

Thank you very much for your time and advice.

Best,
Grayson

Just to be clear: you did run conda activate imagej and the activation appeared to work? And then when you run:

python -c 'import imagej'

It yields an error message like No module named 'imagej'?

What does conda list | grep pyimagej say? It should read something like:

pyimagej                  0.4.0                 py37_1001    conda-forge

If that command returns nothing, then the pyimagej module is not successfully installed into the active conda environment.

In general when troubleshooting technical problems, it is a good idea to paste the exact commands you are running and the exact error messages, rather than paraphrasing them. It helps us be more precise and understand exactly what is going on.

1 Like

Thank you for your quick response and advice. I’ll be more specific this time with what is happening.
This morning I tried the commands that you gave me and no errors were thrown and it outputs what you would expect. Here is a picture of the IDE I’m using with the terminal commands and also where the error (in the .py file) occurs that I’m trying to deal with.

I’m assuming that conda activate imagej is working because of the change before my computers name during the terminal commands.

The problem that I’m running in to is that my python project is not recognizing the imagej module after I activate and import it on the local terminal.

Thank you in advance for your help and suggestions. I really appreciate it.

@Grayson_Harrington PyCharm will not automatically know which Conda environment to use—you’ll have to tell it. I have never tried using a conda environment with PyCharm, but maybe these instructions help you? I expect you will need to use the “Existing environment” option mentioned there to point PyCharm to the imagej conda environment. You can find the directory of the environment by running echo "$CONDA_PREFIX" from the active environment’s terminal.

Maybe someone more versed in Python development here can respond more authoritatively.

Unfortunately, pycharm only uses the interpreter of a (conda) environment but does not inherit any environment variables. You’d have to set those manually for each application you run in the run configurations.

@ctrueden
I double checked which environment I was using and made sure that it am using the imagej conda environment. In the interpreter preferences I see the pyimagej package, but I am still receiving the same "ModuleNotFoundError: No module named ‘imagej’ " error. (See image below)

Should I start fresh with a new environment and try the terminal commands to install pyimagej again? What would you recommend at this point?

@hanslovsky
Thank you for your input. Could you elaborate some on how to do that?

Thank you!

You will need to set PYJNIUS_JAR and JAVA_HOME as in the relevant conda environment

1 Like

Currently, yes. Although we are working to eliminate that requirement. See scijava/scyjava#5 and scijava/scyjava#7 if interested in the details.

@Grayson_Harrington

You can also use the EnvFile plugin to set the environment variables. This lets you define them once and then simply reference the file anytime you need to use pyimagej in the future.

The referenced file is a text file with a particular extension (I use .env). I put the PYJNIUS_JAR and JAVA_HOME variables in there and the system path

JAVA_HOME=C:\Program Files\Java\jdk1.8.0.91\
PYJNIUS_JAR=C:\Users\mpinkert\ ... pjnius\pyjnius.jar 
PATH= ... etc
2 Likes

Hi all,

Thanks so much for all the help so far. So I’ve run into the exact same problem, except I’ve really got no idea what I’m doing, compared with the original poster…Anyhow, I’ve managed to set PYJNIUS_JAR and JAVA_HOME as in the relevant conda environment I believe (see attached photo), but still get the "ModuleNotFoundError: No module named ‘imagej’ " error. Any suggestions? Thanks!!