Cv2 package doesn't work

I am trying to use the cv2 package on my jupyter notebook that is running in a unique conda environment (I named it MNIST). I have tried the following installs but they don’t work:

pip install numpy opencv-python
conda install -c conda-forge opencv
conda install -c menpo opencv

You aren’t providing much to go on here. Why did you try both pip and conda? Where did you try those install commands you provided?
(For future reference, if you have Anaconda/conda involved then that is your primary package manager by your opt in to use it. You should only use pip in that case for things that don’t have conda receipes. Otherwise, you’ll create headaches for yourself.)

Assuming you have Anaconda/conda…
In the notebook where you want to use opencv, run the following in a new cell:

%conda install conda-forge::opencv

That is based on here and the use of the magic conda install command.
After that runs successfully (it probably will take quite a while to solved the environment before it completes), you can then restart the kernel and try the import statement.

I didn’t use pip, my mistake. I tried these in the terminal inside my desired conda environment.

Unless I’m missing something, then you weren’t actually acting in the correct environment. You should be able to run %conda list from inside the running notebook to gain some insight as to what the environment where the kernel is running sees installed, before you try the commands I suggested. (Something I could be missing is that you didn’t share of the install processed fully.) Using the magic approach inside the running notebook, may help. (If that does help, I’d be concerned though that you didn’t quite connect Jupyter to your designed environment properly. It requires several steps.)

By install process, do you mean the anaconda/jupyter activation process?

I just ran %conda list and this is what I see

packages in environment at /Users/tobennaudeze/opt/anaconda3:

Name Version Build Channel

_anaconda_depends 2024.02 py311_openblas_1
_tflow_select 2.2.0 eigen
abseil-cpp 20211102.0 hc377ac9_0
absl-py 2.1.0 py311hca03da5_0
aiobotocore 2.7.0 py311hca03da5_0
other packages

Note: you may need to restart the kernel to use updated packages.

also on the kernel/interpreter part, it says ‘ipykernel’.

What is in your %conda list is what is installed via conda. Do you see the one you are looking for, opencv?

So if you don’t see opencvin that list …
Next try what I said for running %conda install conda-forge::opencv in the notebook earlier?

It takes a while to run/install even when I do it on my terminal. Is there a reason/fix for this?

That is really a different question then the topic.
You should look into discussion about mamba and pixi as alternatives to conda.

1 Like

I’ll let you know if it finishes running?

Hmmm…it shouldn’t take hours, I don’t think.

could it be because I ran the command locally? (not on a GPU and from an installed IDE on a MacBook)

I don’t think so. The installation could take a lone time but not this long.
Is it stuck on the solving?

I’m not sure what you mean by any of this? Do you mean you tried from in Jupyter like I said?
Did the installation work in the terminal when you ran it in the desired environment? You never said what it did, just that it failed. You seem to not be providing any specifics, as I pointed out in the very first sentence of my reply.

yes it was stuck on solving
I ran it on Jupyter Notebook, but not on the web server it was on Pycharm’s IDE. When I tried it on the terminal in the environment I wanted it was also stuck on solving, and stayed there. Sorry for not specifying the details I was a little confused myself.

edit: I re-ran it and it is now examining some conflicts

I have made some progress:
I ran

conda install conda-forge::opencv

in the terminal and it successfully installs opencv in the desired conda environment. I now added the environment to jupyter with

python -m ipykernel install --user --name=mnist_env --display-name “Python (mnist_env)”

now I get this ‘no such file error’

ImportError: dlopen(/Users/tobennaudeze/opt/anaconda3/envs/MNIST/lib/python3.9/site-packages/cv2.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/liblapack.3.dylib
Referenced from: /Users/tobennaudeze/opt/anaconda3/envs/MNIST/lib/libopencv_core.4.5.1.dylib
Reason: tried: ‘/Users/tobennaudeze/opt/anaconda3/envs/MNIST/lib/liblapack.3.dylib’ (no such file), ‘/Users/tobennaudeze/opt/anaconda3/envs/MNIST/lib/liblapack.3.dylib’ (no such file), ‘/Users/tobennaudeze/opt/anaconda3/envs/MNIST/lib/liblapack.3.dylib’ (no such file), ‘/Users/tobennaudeze/opt/anaconda3/envs/MNIST/lib/python3.9/site-packages/…/…/liblapack.3.dylib’ (no such file), ‘/Users/tobennaudeze/opt/anaconda3/envs/MNIST/lib/python3.9/site-packages/…/…/liblapack.3.dylib’ (no such file), ‘/Users/tobennaudeze/opt/anaconda3/envs/MNIST/bin/…/lib/liblapack.3.dylib’ (no such file), ‘/Users/tobennaudeze/opt/anaconda3/envs/MNIST/bin/…/lib/liblapack.3.dylib’ (no such file), ‘/usr/local/lib/liblapack.3.dylib’ (no such file), ‘/usr/lib/liblapack.3.dylib’ (no such file, not in dyld cache)

This seems like progress even if still being not easy.
So the one other place it makes sense to try pip install is when you are having issues and want to troubleshoot and are willing then to sacrifice robustness and portability.
This thread seems related, even thought it is a different package. It may be worth trying in Jupyter:

%pip install --upgrade --force-reinstall opencv-python

See that link for more info.

I just went through the thread and also tried the pip command they/you suggested. I got a new error that wasn’t about cv2. So I deleted my the conda environment (I don’t have much in there because it’s pretty new) and want to start again

Sounds like a good avenue to pursue at this point.
I’d also first look around at advice for installing OpenCV and other related packages on your chip set that your Mac has. Apple’s M-series Silicon processors bring different challenges, and so make sure it is current and related to those specifically.

IT WORKED
I have cv2 installed and everything seems to be fine so far. Thanks so much for the help!!!

While the details are still fresh, can you add what helped you compared to earlier? It sounds like starting with a new Anaconda/conda environment was involved?

yes starting the new environment helped
I created one in the project’s directory with python 3.10, installed Jupyter, then ran the needed installs for my packages.

1 Like