OpenCV 无法使用 anaconda 在根目录外导入

OpenCV Failed to Import outside the root directory using anaconda

重现错误的步骤:

我刚刚在我的新 Conda 环境 (Python 3.7.11) 上安装了 OpenCV,使用:

conda install opencv

一切顺利。 Conda 正在下载和提取一些其他包,包括 numpy-1.21.2. 安装的 opencv 版本是 3.4.2

我试图从我的终端,从根目录运行这个代码。

python
>> import cv2
>> cv2.__version__

还是运行宁顺利。 Python 打印 cv2 版本。但是当我 运行 来自另一个目录的相同代码时出现错误(例如:~/Downloads)。错误是:

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

更多奇怪的错误

当我运行同样的代码直接从vscode和select同样的conda环境中vscode,同样的问题出现在vscode 终端。更重要的是,当我尝试从 vscode 终端输入 python 时,它显示 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.
>>> 

一些我已经尝试过的步骤:

没有任何效果。需要你们的帮助。

系统规格

如果你已经激活了conda环境。它的名称将添加在命令行的开头。那么当你搜索python命令时,激活环境中的python可执行文件将是第一个

并且可以通过pip show opencv-python命令找到opencv模块。

来自你问题中的第二个代码。看起来你的 numpy 模块有问题,你可以尝试重新安装 (pip install -U numpy) 它吗?你能用pip install -U opencv-python命令升级你的opencv吗?因为我的cv2版本是4.5.4-dev,效果很好