Opencv - ModuleNotFoundError: 没有名为'cv2'的模块 - MacOS

问题描述 投票:0回答:1

我是python新手,想用Opencv运行一个项目。

要求.txt

Keras>=2.2.4
numpy>=1.16.2
opencv-python>=4.0.0.21
Pillow>=5.4.1
tensorflow>=1.12.0

运行下面的命令。

pip3.8 install -r requirements.txt

Collecting Keras>=2.2.4
  Downloading Keras-2.4.1-py2.py3-none-any.whl (169 kB)
     |████████████████████████████████| 169 kB 798 kB/s 
Collecting numpy>=1.16.2
  Downloading numpy-1.18.5-cp38-cp38-macosx_10_9_x86_64.whl (15.1 MB)
     |████████████████████████████████| 15.1 MB 3.2 MB/s 
Collecting opencv-python>=4.0.0.21
  Downloading opencv_python-4.2.0.34-cp38-cp38-macosx_10_9_x86_64.whl (49.1 MB)
     |████████████████████████████████| 49.1 MB 5.9 MB/s 
Collecting Pillow>=5.4.1
  Downloading Pillow-7.1.2-cp38-cp38-macosx_10_10_x86_64.whl (2.2 MB)
     |████████████████████████████████| 2.2 MB 10.0 MB/s 
Collecting tensorflow>=1.12.0
  Downloading tensorflow-2.2.0-cp38-cp38-macosx_10_11_x86_64.whl (175.4 MB)
     |████████████████████████████████| 175.4 MB 130 kB/s 
Collecting pyyaml
  Downloading PyYAML-5.3.1.tar.gz (269 kB)
     |████████████████████████████████| 269 kB 10.7 MB/s 
Collecting h5py
  Downloading h5py-2.10.0-cp38-cp38-macosx_10_9_x86_64.whl (3.0 MB)
     |████████████████████████████████| 3.0 MB 9.6 MB/s 
Collecting scipy>=0.14
  Downloading scipy-1.4.1-cp38-cp38-macosx_10_9_x86_64.whl (28.8 MB)
     |████████████████████████████████| 28.8 MB 891 kB/s 
Collecting tensorflow-estimator<2.3.0,>=2.2.0
  Downloading tensorflow_estimator-2.2.0-py2.py3-none-any.whl (454 kB)
     |████████████████████████████████| 454 kB 9.5 MB/s 
Collecting astunparse==1.6.3
  Downloading astunparse-1.6.3-py2.py3-none-any.whl (12 kB)
Collecting keras-preprocessing>=1.1.0
  Downloading Keras_Preprocessing-1.1.2-py2.py3-none-any.whl (42 kB)
     |████████████████████████████████| 42 kB 3.4 MB/s 
Collecting opt-einsum>=2.3.2
  Downloading opt_einsum-3.2.1-py3-none-any.whl (63 kB)
     |████████████████████████████████| 63 kB 6.5 MB/s 
Collecting wrapt>=1.11.1
  Downloading wrapt-1.12.1.tar.gz (27 kB)
Collecting six>=1.12.0
  Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting grpcio>=1.8.6
  Downloading grpcio-1.29.0-cp38-cp38-macosx_10_9_x86_64.whl (2.8 MB)
     |████████████████████████████████| 2.8 MB 8.7 MB/s 
Collecting termcolor>=1.1.0
  Downloading termcolor-1.1.0.tar.gz (3.9 kB)
Collecting protobuf>=3.8.0
  Downloading protobuf-3.12.2-cp38-cp38-macosx_10_9_x86_64.whl (1.3 MB)
     |████████████████████████████████| 1.3 MB 10.9 MB/s 
Collecting absl-py>=0.7.0
  Downloading absl-py-0.9.0.tar.gz (104 kB)
     |████████████████████████████████| 104 kB 11.7 MB/s 
Collecting gast==0.3.3
  Downloading gast-0.3.3-py2.py3-none-any.whl (9.7 kB)
Collecting google-pasta>=0.1.8
  Downloading google_pasta-0.2.0-py3-none-any.whl (57 kB)
     |████████████████████████████████| 57 kB 7.0 MB/s 
Requirement already satisfied: wheel>=0.26; python_version >= "3" in /usr/local/lib/python3.8/site-packages (from tensorflow>=1.12.0->-r requirements.txt (line 5)) (0.34.2)
Collecting tensorboard<2.3.0,>=2.2.0
  Downloading tensorboard-2.2.2-py3-none-any.whl (3.0 MB)
     |████████████████████████████████| 3.0 MB 4.6 MB/s 
Requirement already satisfied: setuptools in /usr/local/lib/python3.8/site-packages (from protobuf>=3.8.0->tensorflow>=1.12.0->-r requirements.txt (line 5)) (46.0.0)
Collecting requests<3,>=2.21.0
  Downloading requests-2.24.0-py2.py3-none-any.whl (61 kB)
     |████████████████████████████████| 61 kB 1.3 MB/s 
Collecting google-auth-oauthlib<0.5,>=0.4.1
  Downloading google_auth_oauthlib-0.4.1-py2.py3-none-any.whl (18 kB)
Collecting werkzeug>=0.11.15
  Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
     |████████████████████████████████| 298 kB 12.7 MB/s 
Collecting tensorboard-plugin-wit>=1.6.0
  Downloading tensorboard_plugin_wit-1.6.0.post3-py3-none-any.whl (777 kB)
     |████████████████████████████████| 777 kB 11.6 MB/s 
Collecting markdown>=2.6.8
  Downloading Markdown-3.2.2-py3-none-any.whl (88 kB)
     |████████████████████████████████| 88 kB 7.8 MB/s 
Collecting google-auth<2,>=1.6.3
  Downloading google_auth-1.18.0-py2.py3-none-any.whl (90 kB)
     |████████████████████████████████| 90 kB 7.3 MB/s 
Collecting chardet<4,>=3.0.2
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
     |████████████████████████████████| 133 kB 10.8 MB/s 
Collecting certifi>=2017.4.17
  Downloading certifi-2020.4.5.2-py2.py3-none-any.whl (157 kB)
     |████████████████████████████████| 157 kB 11.2 MB/s 
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Downloading urllib3-1.25.9-py2.py3-none-any.whl (126 kB)
     |████████████████████████████████| 126 kB 8.7 MB/s 
Collecting idna<3,>=2.5
  Downloading idna-2.9-py2.py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 7.3 MB/s 
Collecting requests-oauthlib>=0.7.0
  Downloading requests_oauthlib-1.3.0-py2.py3-none-any.whl (23 kB)
Collecting cachetools<5.0,>=2.0.0
  Downloading cachetools-4.1.0-py3-none-any.whl (10 kB)
Collecting pyasn1-modules>=0.2.1
  Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
     |████████████████████████████████| 155 kB 10.3 MB/s 
Collecting rsa<5,>=3.1.4; python_version >= "3"
  Downloading rsa-4.6-py3-none-any.whl (47 kB)
     |████████████████████████████████| 47 kB 8.8 MB/s 
Collecting oauthlib>=3.0.0
  Downloading oauthlib-3.1.0-py2.py3-none-any.whl (147 kB)
     |████████████████████████████████| 147 kB 8.8 MB/s 
Collecting pyasn1<0.5.0,>=0.4.6
  Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
     |████████████████████████████████| 77 kB 4.2 MB/s 
Building wheels for collected packages: pyyaml, wrapt, termcolor, absl-py
  Building wheel for pyyaml (setup.py) ... done
  Created wheel for pyyaml: filename=PyYAML-5.3.1-cp38-cp38-macosx_10_15_x86_64.whl size=44625 sha256=d7e2d69327685a3906c391379671ed91d691e1adf64f57f5161efd445291197b
  Stored in directory: /Users/baskar/Library/Caches/pip/wheels/13/90/db/290ab3a34f2ef0b5a0f89235dc2d40fea83e77de84ed2dc05c
  Building wheel for wrapt (setup.py) ... done
  Created wheel for wrapt: filename=wrapt-1.12.1-py3-none-any.whl size=19553 sha256=383c8239fb5627cf262ddfb670dac377005cd4e26ea995d45dfe67aba71599df
  Stored in directory: /Users/baskar/Library/Caches/pip/wheels/5f/fd/9e/b6cf5890494cb8ef0b5eaff72e5d55a70fb56316007d6dfe73
  Building wheel for termcolor (setup.py) ... done
  Created wheel for termcolor: filename=termcolor-1.1.0-py3-none-any.whl size=4830 sha256=4e33c05eeb7d783c2a7616f4499fca92478018d808dc3eaa1ac86388170d1f35
  Stored in directory: /Users/baskar/Library/Caches/pip/wheels/a0/16/9c/5473df82468f958445479c59e784896fa24f4a5fc024b0f501
  Building wheel for absl-py (setup.py) ... done
  Created wheel for absl-py: filename=absl_py-0.9.0-py3-none-any.whl size=121931 sha256=6d0a35077dd7a894ed91184ccb8b587694422ba4f80ec3ffb640eec35f720479
  Stored in directory: /Users/baskar/Library/Caches/pip/wheels/1d/10/8e/2f79b924179ff1e6510933d63eb851bea01054fff262343b7a
Successfully built pyyaml wrapt termcolor absl-py
Installing collected packages: pyyaml, tensorflow-estimator, six, astunparse, numpy, keras-preprocessing, opt-einsum, wrapt, h5py, grpcio, scipy, termcolor, protobuf, absl-py, gast, google-pasta, chardet, certifi, urllib3, idna, requests, oauthlib, requests-oauthlib, cachetools, pyasn1, pyasn1-modules, rsa, google-auth, google-auth-oauthlib, werkzeug, tensorboard-plugin-wit, markdown, tensorboard, tensorflow, Keras, opencv-python, Pillow
Successfully installed Keras-2.4.1 Pillow-7.1.2 absl-py-0.9.0 astunparse-1.6.3 cachetools-4.1.0 certifi-2020.4.5.2 chardet-3.0.4 gast-0.3.3 google-auth-1.18.0 google-auth-oauthlib-0.4.1 google-pasta-0.2.0 grpcio-1.29.0 h5py-2.10.0 idna-2.9 keras-preprocessing-1.1.2 markdown-3.2.2 numpy-1.18.5 oauthlib-3.1.0 opencv-python-4.2.0.34 opt-einsum-3.2.1 protobuf-3.12.2 pyasn1-0.4.8 pyasn1-modules-0.2.8 pyyaml-5.3.1 requests-2.24.0 requests-oauthlib-1.3.0 rsa-4.6 scipy-1.4.1 six-1.15.0 tensorboard-2.2.2 tensorboard-plugin-wit-1.6.0.post3 tensorflow-2.2.0 tensorflow-estimator-2.2.0 termcolor-1.1.0 urllib3-1.25.9 werkzeug-1.0.1 wrapt-1.12.1

现在,尝试运行下面的代码。

python3.7 main.py,得到以下错误。

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

代码。

import argparse
import sys

import cv2
import keras
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from keras.models import load_model
....

这里有什么问题吗?

python-3.x opencv opencv3.0
1个回答
1
投票

我相信这是因为你使用的是 pip3.8 安装要求和 python3.7 来运行这个文件。我认为包在不同的python版本中是不共享的。为了解决你的问题,我认为你应该用以下方法运行文件 python3.8 或安装要求与 pip3.7.

希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.