Import error with OPENCV in Python on M1 Mac - python

I'm trying to use OpenCV with Python but I'm continuously getting errors while importing. I'm using Anaconda virtual env and ZSH on Mac M1.
I have tried installing it using
conda install -c anaconda opencv
and pip3 install opencv-python
but I'm getting this error:
from .cv2 import * ImportError: dlopen(/Users/username/Library/Python/3.8/lib/python/site-packages/cv2/cv2.cpython-38-darwin.so, 0x0002): Library not loaded: /opt/homebrew/opt/ffmpeg/lib/libavcodec.58.dylib Referenced from: /Users/username/Library/Python/3.8/lib/python/site-packages/cv2/cv2.cpython-38-darwin.so Reason: tried: '/opt/homebrew/opt/ffmpeg/lib/libavcodec.58.dylib' (no such file), '/usr/local/lib/libavcodec.58.dylib' (no such file), '/usr/lib/libavcodec.58.dylib' (no such file)

First you should make sure that ffmpeg is installed.
for me the issue was that the ffmpeg which I had installed by brew was named like ffmpeg#4 in opt/homebrew/opt/ so it couldn't find it.
I renamed the folder to ffmpeg and the issue was fixed.

Related

Openssl issue with python 3.8

I am trying to use a python package wptools. When I do I get this error:
import pycurl
ImportError: dlopen(/Users/c/.virtualenvs/j/lib/python3.8/site-packages/pycurl.cpython-38-darwin.so, 2): Library not loaded: #rpath/libssl.1.1.dylib
Referenced from: /Users/c/.virtualenvs/j/lib/python3.8/site-packages/pycurl.cpython-38-darwin.so
Reason: image not found
I have tried all kinds of things, such installing with brew install openssl. What is weird is when I open a terminal on my mac, which uses python 3.7.6, it works. But when I try it in a virtual env, which is 3.8.10, it does not work. Any ideas?

import pycurl: Library not loaded: #rpath/libcrypto.1.1.dylib : Reason: image not found

My flask app python code error's out with below error :
Traceback (most recent call last):
File "######.py",
import pycurl
ImportError: dlopen(/Users/########/Library/Python/3.7/lib/python/site-packages/pycurl.cpython-37m-darwin.so, 2): Library not loaded: #rpath/libcrypto.1.1.dylib
Referenced from: /Users/########/Library/Python/3.7/lib/python/site-packages/pycurl.cpython-37m-darwin.so
Reason: image not found
OS: Darwin Kernel Version 18.6.0 - Mojave 10.14.5
Tried updating brew,
openssl (link and unlink, export LDFLAGS, export CFFFLAGS) but nothing appears to work
$ brew install openssl
openssl 1.0.2s is already installed and up-to-date
$brew install curl-openssl
curl-openssl 7.65.3 is already installed and up-to-date
Any additional pointers to try. Thanks in advance
What solved it in my case was changing the path where Python would look for this libs, as they were installed by brew. But you can tell it to look for the lib somewhere else:
Add the following at the bottom of your profile file (~/.bashrc or ~/.zshrc, depending on your shell):
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/opt/local/lib
Then restart your shell, and try again (remember to reload your venv if your were using one).

Why is pip installing Pillow for OS X 10.12, when I have OS X 10.11 installed?

When trying to import PIL (using Pillow), I get the following error:
from PIL import ImageTk, Image
File "/usr/local/lib/python2.7/site-packages/PIL/ImageTk.py", line 42, in <module>
from . import Image
File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 60, in <module>
from . import _imaging as core
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/PIL/_imaging.so, 2): Symbol not found: _clock_gettime
Referenced from: /usr/local/lib/python2.7/site-packages/PIL/.dylibs/liblzma.5.dylib (which was built for Mac OS X 10.12)
Expected in: /usr/lib/libSystem.B.dylib
in /usr/local/lib/python2.7/site-packages/PIL/.dylibs/liblzma.5.dylib
Note the line Referenced from: /usr/local/lib/python2.7/site-packages/PIL/.dylibs/liblzma.5.dylib (which was built for Mac OS X 10.12) and specifically '(which was built for Mac OS X 10.12)'.
I have a hunch that I may have corrupted something when I attempted to manually install the wrong version of xCode (not compatible with 10.11.6, which is on this computer). I have installed Pillow using pip install Pillow - but that installation resulted in this error. Is there a way to force pip to install a certain version of Pillow, to see if the problem lies in pip installing a problematic version?
This is a bug in the latest Pillow 5.1.0 release.
It was caused by upgrading Xcode from version 8 to 9.2 for building the binary wheels.
El Capitan 10.11 is the "min macOS to run" for Xcode 8.
Sierra 10.12.6 is the min for Xcode 9.2.
https://en.wikipedia.org/wiki/Xcode#Xcode_7.0_-9.x(since_Free_On-Device_Development
There will be a Pillow 5.1.1 out to fix it at some point.
In the meantime, the workaround is: pip install 'pillow!=5.1.0'
(Or upgrade your macOS, or build from source.)
For more info, see https://github.com/python-pillow/Pillow/issues/3068
That sounds like the wheel uploaded to PyPI was not built for your platform correctly. Unfortunately having the wheel for a PyPI package for your platform is not a guarantee that the wheel will install or work correctly, you're at the mercy of the PyPI package maintainer in this regard.
You should run pip uninstall Pillow, download the source directly from PyPI, build it manually and run pip install -e <path to rebuilt package> to install the wheel you built specifically for your machine.

ImportError: No module named cv2 after install VS2017

Before:
Install VS2015.
Install opencv 3.2.0.
Now, i install VS2017 and create python project with the following code:
import cv2
When I run it, compiler show the error: "ImportError: No module named cv2"
How to update some libraries - which i installed before - as opencv, numpy,... for VS2017?
Thanks!
UPDATE 1
I knew the reason. Because when i installed VS2017, i also installed other version of Python. VS2017 used the wrong version (it does not have opencv library). And then i created new universal windows python project, VS2017 auto generate PythonUwpIoT (python 3.5 - i don't know and how to have it).

Why can't I import opencv3 even though the package is installed?

I currently am running Python 3.5 and using Spyder from Anaconda as my IDE. I am running this on a Windows machine.
When I write import cv3 at the top of my code, it returns the error ImportError: No module named 'cv3'
I attempted to install opencv3 again with the command conda install -c https://conda.binstar.org/menpo opencv3 in the Command Prompt. It is apparently already installed because it returned
Fetching package metabase...............
Solving package specifications: .
# All requested packages already installed.
# packages in environment at C:\Users\Joey\Anaconda3:
# opencv3 3.1.0 py35_0 https://conda.binstar.org/menpo
Am I importing cv3 wrong? How do I fix this error?
Update: Tried import cv3 instead of import cv2 but got the following error: ImportError: cannot import name 'cv2'. The wording on the two errors is different, so python must acknowledge there is opencv installed but it does not work for some reason. Any ideas?
Ironically enough, the module is still called cv2 because it doesn't represent the version of opencv but the actual C++ API underneath which is, to be contrasted from the C API, named - cv2... So try with: import cv2
Problem solved by using command pip uninstall opencv-python in the Command Prompt.
I have attempted several installations of opencv and I suppose one may have downloaded badly and Anaconda was attempting to read that one. I looked into the build of some of the other installations I attempted and some were for Python 2.7. Maybe that contributed to the error.
Thankfully, this worked. Now import cv2 works perfectly. No errors.
I used the same approach to install the package. However, I could not import the library using the name opencv3. I had to use cv2 which worked for me.
Elaborating on #zwer's answer, check the version of OpenCV after import cv2.
>>> cv2.__version__
'3.1.0'
So basically it's calling the OpenCV3 library.

Categories