可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have installed OpenCV on the Occidentalis operating system (a variant of Raspbian) on a Raspberry Pi, using jayrambhia\'s script found here: https://github.com/jayrambhia/Install-OpenCV/blob/master/Ubuntu/opencv_latest.sh. It installed version 2.4.5.
When I try to import the module cv2 in a python program, I get the following message:
pi@raspberrypi~$ python cam.py
Traceback (most recent call last)
File \"cam.py\", line 1, in <module>
import cv2
ImportError: No module named cv2
cv2.so is stored in
/usr/local/lib/python2.7/site-packages/cv2.so
There are also folders in /usr/local/lib called python3.2 and python2.6, which could be a problem but I\'m not sure.
Is this a path error perhaps? Any help appreciated (new to Linux).
Thanks.
回答1:
First do run these commands inside Terminal/CMD:
conda update anaconda-navigator
conda update navigator-updater
then the issue for the instruction below will be resolved
for windows if you have anaconda installed, you can simply do
pip install opencv-python
or
conda install -c https://conda.binstar.org/menpo opencv
if you are on linux you can do :
pip install opencv-python
or
conda install opencv
Link1 Link2
For python3.5+ check these links : Link3 , Link4
回答2:
This happens when python cannot refer to your default site-packages folder where you have kept the required python files or libraries
Add these lines in the code:
import sys
sys.path.append(\'/usr/local/lib/python2.7/site-packages\')
or before running the python command in bash move to /usr/local/lib/python2.7/site-packages directory. This is a work around if you don\'t want to add any thing to the code.
回答3:
Try to add the following line in ~/.bashrc
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
回答4:
I solved my issue using the following command :
conda install opencv
回答5:
Python3.x User
sudo pip3 install opencv-python
回答6:
None of the above answers worked for me. I was going crazy until I found this solution below!
Simply run:
sudo apt install python-opencv
回答7:
If want to install opencv in virtual environment.
Run command in terminal for getting virtual environment list.
conda env list
or
jupyter notebook command is
!conda env list
Then update your anaconda.
conda update anaconda-navigator
conda update navigator-updater
Install opencv in your selected environment path.
conda install -c [\'environment path\'] opencv
Juypter notebook
!conda install --yes --prefix [\'environment path\'] opencv
回答8:
I have come accross same as this problem i installed cv2 by
pip install cv2
However when i import cv2 module it displayed no module named cv2 error.
Then i searched and find cv2.pyd
files in my computer and i copy and paste to site-packages directory
C:\\Python27\\Lib\\site-packages
then i closed and reopened existing application, it worked.
EDIT
I will tell how to install cv2
correctly.
1. Firstly install numpy on your computer by
pip install numpy
2. Download opencv from internet (almost 266 mb).
I download opencv-2.4.12.exe
for python 2.7. Then install this opencv-2.4.12.exe file.
I extracted to C:\\Users\\harun\\Downloads
to this folder.
After installation go look for cv2.py
into the folders.
For me
C:\\Users\\harun\\Downloads\\opencv\\build\\python\\2.7\\x64
in this folder take thecv2.pyd
and copy it in to the
C:\\Python27\\Lib\\site-packages
now you can able to use cv2
in you python scripts.
回答9:
You get No module named cv2.cv
.
Son, you did all step right, since your sudo make install
gave no errors.
However look at this step
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
THE VERY IMPORTANT STEP OF ALL THESE IS TO LINK IT.
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
The moment you choose wise linking, or by brute force just find the cv2.so file if that exist or not
Here I am throwing my output.
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 appinventiv:
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
>>>
My step will only help, if your built is done right.
回答10:
Try this out:
sudo ldconfig
sudo nano /etc/ld.so.conf.d/opencv.conf
and add this following line in the opencv.conf
not in the command window
/usr/local/lib
Then:
sudo ldconfig
sudo nano /etc/bash.bashrc
and add this two lines in the bash.bashrc
not in the command window
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
at last reboot your Pi sudo reboot now
and try import cv2
回答11:
I had the same problem, just couldn\'t figure it out with opencv2 and opencv3 installed into /opt/opencv and opencv3 respectively. Turned out that bloody anaconda install of opencv in my home directory was first on path and mangled opencv. Removed it and started using /opt/opencv3/lib as defined in /etc/ld.so.conf.d/opencv.conf. Worked perfectly first go. Do you have anaconda installed? Could be the issue.