简体   繁体   中英

Importing OpenCV - import cv2 or cv3, and why does the directory matter?

I am trying to use OpenCV for Python 3 for the first time. To install, I simply typed "pip3 install opencv-python" into the terminal. When I do this, I see in Finder (I'm on a Mac), that in /usr/local/lib/python3.7/site-packages, I now have a new folder called "cv2". This is strange. Why isn't it cv3, as I used pip3? I do not know.

When I try to import cv2 from python3, it says "No module named 'cv2'". Luckily StackOverflow already has a solution : if I first change directories like: "cd /usr/local/lib/python3.7/site-packages" then I will be able to open cv2. I do not know why this step is necessary for OpenCV. I don't have to do this to "import numpy" for example; "numpy" does not care what directory I start from, and it is listed like just another module like cv2.

Anyways, I change directory to site-packages, start python3, and I import cv2. Now there is a new error. This time cv2 is found, but it says:

"from .cv2 import *
ModuleNotFoundError: No module named 'cv2.cv2'"

For the record, I also have Python 2.7. If I try "cd /usr/local/lib/python2.7/site-packages" and then start Python 2.7, I can import cv2. This is ok, but I want to use OpenCV with Python3.

Let me summarize.

(1) How can I get cv2 or cv3 to import when I start from any directory? Is this ever possible?

(2) How can I get cv3 for python3? cv2 seems to be problematic in python3 because because there is no "cv2.cv2"

没有 cv3,只有导入 cv2 ,opencv 旧版或最新版是针对旧 python 或最新版更新的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM