重現錯誤的步驟:
我剛剛使用以下命令在我的新 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運行相同的代碼并在vscode中選擇相同的conda環境時,在vscode終端中出現相同的問題。更重要的是,當我嘗試python從 vscode 終端輸入時,它顯示 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.
>>>
我已經嘗試過的一些步驟:
- 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
uj5u.com熱心網友回復:
如果您已激活 conda 環境。它的名稱將被添加到命令列的開頭。那么當你搜索python命令時,激活環境中的python可執行檔案將是第一個。

您可以opencv使用 pip show 命令找到該模塊opencv-python。

來自您問題中的第二個代碼。看起來您的numpy模塊有問題,您可以嘗試重新安裝( pip install -U numpy)嗎?你能用命令升級你的opencvpip install -U opencv-python嗎?因為我的 cv2 版本是4.5.4-dev,而且效果很好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348693.html
