Home > OS >  OpenCV Failed to Import outside the root directory using anaconda
OpenCV Failed to Import outside the root directory using anaconda

Time:11-04

Steps to reproduce the error:

I have just installed OpenCV on my new Conda environment (Python 3.7.11) using :

conda install opencv

Everything went well. Conda was downloading and extracting some other packages, including numpy-1.21.2. The installed opencv version is 3.4.2

I tried to run this code from my terminal, from the root directory.

python
>> import cv2
>> cv2.__version__

Still running smoothly. Python prints the cv2 version. But there is an errorwhen I run the same code from another directory (e.g. : ~/Downloads). The error is :

Python 3.7.11 (default, Jul 27 2021, 07:03:16)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import

More Weird Errors

When I run the same code directly from vscode and select the same conda environment in the vscode, the same problems appear in the vscode terminal. Even more, when I try to type python from the vscode terminal, it shows python 2.7

 │  ~  source /opt/anaconda3/bin/activate                                                                       1 ✘ │ base  │ 08:09:48 PM  

  │  ~  conda activate testenvi                                                                                    ✔ │ base  │ 08:09:48 PM  

  │  ~  python -u "/Users/martinmanullang/Downloads/testsiopencv.py"                                           ✔ │ testenvi  │ 08:09:49 PM  
Traceback (most recent call last):
  File "/Users/martinmanullang/Downloads/testsiopencv.py", line 1, in <module>
    import cv2
ImportError: No module named cv2

  │  ~  python                                                                                               1 ✘ │ testenvi  │ 08:09:53 PM  

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.18 (default, Oct  2 2021, 04:20:39) 
[GCC Apple LLVM 13.0.0 (clang-1300.0.29.1) [ internal-os, ptrauth-isa=deploymen on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Some steps that I already tried:

  • Reinstalling opencv with pip install opencv-contrib-python
  • Reinstalling numpy (with pip, with conda)
  • Create new env with python 3.5,3.6,3.9

Nothing works. Kindly need help from you guys.

System Specification

  • MacbookPro 2020 running macOS Monterey
  • VSCode 1.61.2
  • iTerm 3.4.12 with ZSH

CodePudding user response:

If you have activated the conda environment. Its name will be added at the head of the command line. Then when you search the python command, the python executable in the activated environment will be the first one.

enter image description here

And you can find the opencv module with the command of pip show opencv-python.

enter image description here

From the second code in your question. It looks like your numpy module has some problem, Could you try to reinstall(pip install -U numpy) it? And could you upgrade your opencv with the command of pip install -U opencv-python? Because my version of cv2 was 4.5.4-dev, and it works well.

  • Related