Cannot find module cv2 when using OpenCV
我在Raspberry Pi上的Occidentalis操作系统(Raspbian的一个变种)上安装了OpenCV,使用了jayrambhia的脚本。 它安装了2.4.5版本。
当我在Python程序中尝试
1 2 3 4 5 | pi@raspberrypi~$ python cam.py Traceback (most recent call last) File"cam.py", line 1, in <module> import cv2 ImportError: No module named cv2 |
文件
这可能是路径错误吗? 任何帮助表示赞赏,我是Linux新手。
首先在Terminal / CMD中运行这些命令:
1 2 | conda update anaconda-navigator conda update navigator-updater |
然后将解决以下说明的问题
对于Windows,如果你安装了anaconda,你可以简单地做
1 | pip install opencv-python |
要么
1 | conda install -c https://conda.binstar.org/menpo opencv |
如果你在Linux上,你可以这样做:
1 | pip install opencv-python |
要么
1 | conda install opencv |
Link1 Link2
对于python3.5 +,请检查以下链接:Link3,Link4
当python无法引用您保留所需python文件或库的默认site-packages文件夹时,会发生这种情况
在代码中添加以下行:
1 2 3 | import sys sys.path.append('/usr/local/lib/python2.7/site-packages') |
或者在bash中运行python命令之前移动到/usr/local/lib/python2.7/site-packages目录。如果您不想在代码中添加任何内容,这是一种解决方法。
我使用以下命令解决了我的问题:
1 | conda install opencv |
尝试在
1 | export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH |
Python3.x用户
1 | sudo pip3 install opencv-python |
上述答案都不适合我。我发疯了,直到我在下面找到这个解决方案!
简单地运行:
1 | sudo apt install python-opencv |
我使用以下命令解决了我的问题:
1 | pip install opencv-python |
我遇到了这个问题我安装了cv2 by
1 | pip install cv2 |
但是,当我导入cv2模块时,它没有显示名为cv2错误的模块。
然后我搜索并在我的计算机中找到
1 | C:\Python27\Lib\site-packages |
然后我关闭并重新打开现有的应用程序,它工作。
编辑
我将告诉你如何正确安装
1.首先在你的计算机上安装numpy
1 | pip install numpy |
2.从互联网上下载opencv(差不多266 mb)。
我下载
我解压缩到
安装完成后,在文件夹中查找
1 | C:\Users\harun\Downloads\opencv\build\python\2.7\x64 |
在此文件夹中取
1 | C:\Python27\Lib\site-packages |
现在你可以在python脚本中使用
如果想在虚拟环境中安装opencv。
在终端运行命令获取虚拟环境列表。
1 | conda env list |
要么
jupyter notebook命令是
1 | !conda env list |
然后更新你的Python。
1 2 | conda update anaconda-navigator conda update navigator-updater |
在所选环境路径中安装opencv。
1 | conda install -c ['environment path'] opencv |
Juypter笔记本
1 | !conda install --yes --prefix ['environment path'] opencv |
如果您正在从划痕中建立,请通过此操作
你得到
儿子,你做的一切正确,因为你的
不过看看这一步
1 2 | $ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ $ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so |
所有这些都非常重要的一步就是把它联系起来。
1 2 3 | ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so or ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so |
您选择明智链接的那一刻,或者通过暴力破解,只需查找cv2.so文件(如果存在与否)
在这里,我抛出我的输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | Successfully installed numpy-1.15.3 (cv) demonLover-desktop:~$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ pip list Package Version ---------- ------- numpy 1.15.3 pip 18.1 setuptools 40.5.0 wheel 0.32.2 (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ python Python 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2 Type"help","copyright","credits" or"license" for more information. >>> import cv2 Traceback (most recent call last): File"<stdin>", line 1, in <module> ImportError: No module named cv2 >>> [2]+ Stopped python (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls /usr/local/lib/python2.7/site-packages/c ls: cannot access '/usr/local/lib/python2.7/site-packages/c': No such file or directory (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls /usr/local/lib/python2.7/site-packages/ (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ deactivate demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls /usr/local/lib/python2.7/site-packages/ demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls cv2.so easy_install.py easy_install.pyc numpy numpy-1.15.3.dist-info pip pip-18.1.dist-info pkg_resources setuptools setuptools-40.5.0.dist-info wheel wheel-0.32.2.dist-info demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls /usr/local/lib/python2.7/site-packages/ demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls -l /usr/local/lib/python2.7/site-packages/ total 0 demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls cv2.so easy_install.py easy_install.pyc numpy numpy-1.15.3.dist-info pip pip-18.1.dist-info pkg_resources setuptools setuptools-40.5.0.dist-info wheel wheel-0.32.2.dist-info demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ workon cv (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ python Python 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2 Type"help","copyright","credits" or"license" for more information. >>> import cv2 Traceback (most recent call last): File"<stdin>", line 1, in <module> ImportError: No module named cv2 >>> [3]+ Stopped python (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ find / -name"cv2.so" find: ‘/lost+found’: Permission denied find: ‘/run/udisks2’: Permission denied find: ‘/run/docker’: Permission denied find: ‘/run/exim4’: Permission denied find: ‘/run/lightdm’: Permission denied find: ‘/run/cups/certs’: Permission denied find: ‘/run/sudo’: Permission denied find: ‘/run/samba/ncalrpc/np’: Permission denied find: ‘/run/postgresql/9.5-main.pg_stat_tmp’: Permission denied find: ‘/run/postgresql/10-main.pg_stat_tmp’: Permission denied find: ‘/run/lvm’: Permission denied find: ‘/run/systemd/inaccessible’: Permission denied find: ‘/run/lock/lvm’: Permission denied find: ‘/root’: Permission denied ^C (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ sudofind / -name"cv2.so" sudofind: command not found (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ^C (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ sudo find / -name"cv2.so" [sudo] password for app: find: ‘/run/user/1000/gvfs’: Permission denied ^C (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ sudo find /usr/ -name"cv2.so" /usr/local/lib/python2.7/dist-packages/cv2.so ^C (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ln -s /usr/local/lib/python2.7/dist-packages/ccv2.so cv2.so click/ clonevirtualenv.pyc configparser-3.5.0.dist-info/ configparser.py cv2.so cycler.py clonevirtualenv.py concurrent/ configparser-3.5.0-nspkg.pth configparser.pyc cycler-0.10.0.dist-info/ cycler.pyc (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so ln: failed to create symbolic link 'cv2.so': File exists (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ rm cv2.so (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls cv2.so easy_install.py easy_install.pyc numpy numpy-1.15.3.dist-info pip pip-18.1.dist-info pkg_resources setuptools setuptools-40.5.0.dist-info wheel wheel-0.32.2.dist-info (cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ python Python 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2 Type"help","copyright","credits" or"license" for more information. >>> import cv2 >>> |
如果您的构建完成正确,我的步骤将只有帮助。
我使用opencv安装和工作的另一种方式是在visual studio 2017社区内。
Visual Studio有一个很好的python环境和调试。
所以从vs python env窗口我搜索并添加了opencv。
只是想我会分享,因为我喜欢在不同的计算机上尝试不同的方式。
我有同样的问题,只是无法弄清楚opencv2和opencv3分别安装到/ opt / opencv和opencv3。原来,在我的主目录中的血腥anaconda安装opencv是第一个路径和损坏opencv。删除它并开始使用/etc/ld.so.conf.d/opencv.conf中定义的/ opt / opencv3 / lib。先完成工作。你有安装anaconda吗?可能是问题。
试试这个:
1 2 3 | sudo ldconfig sudo nano /etc/ld.so.conf.d/opencv.conf |
并在
1 | /usr/local/lib |
然后:
1 2 3 | sudo ldconfig sudo nano /etc/bash.bashrc |
并在
1 2 | PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH |
最后重启你的Pi
并尝试