Question or problem about Python programming:
I have an Ubuntu 14.04 system, on which I want to install OpenCV and use it with Python 2.x.
I installed OpenCV using the instructions here:
The install seemed to run properly, no errors, the script ended with output
OpenCV 2.4.9 ready to be used
When I try to run the sample Python script, I get the following:
$ python opencv.py Traceback (most recent call last): File "opencv.py", line 1, in
from cv2.cv import * ImportError: No module named cv2.cv
I suspect I know why, I just don’t know how to fix it. OpenCV installed to the current directory I was in when I ran the install script, it’s a subdirectory of my home folder.
Others who get this import error after install seem to be having a path issue, and have luck adding this to their code:
import sys sys.path.append('/usr/local/lib/python2.7/site-packages')
or updating their PYTHONPATH with that same directory. I tried adding that code, it doesn’t make a difference. I don’t see any files in the “site-packages” directory. Should I have done the install in that directory? I imagine the installation instructions would have spelled that out. I suspect that my problem has to do with Python not finding the OpenCV install, but I’m not sure how to proceed.
Please help me get a usable install of OpenCV as simply as possible.
How to solve the problem:
I think you don’t have the
I had the exact same problem and
sudo apt-get install python-opencv
solved the issue for me.
you can install opencv from the following link
It works for me .
apt-get install doesnt contain many packages of opencv
I also had this issue. Tried different things. But finally
conda install opencv
worked for me.
If you want as simple as possible, install from the repository:
sudo apt-get install python-opencv libopencv-dev python-numpy python-dev
$ pip install SomePackage [...] Successfully installed SomePackage
And when you add a path to PYTHONPATH with sys, PYTHONPATH it’s always restarted to default values when you close your Python shell. Check this thread:
Permanently add a directory to PYTHONPATH
First add openCV to your path (Quick guide):
after that, install the non-python packages pyopencv depends on:
sudo apt-get build-dep python-opencv
finally, use pip:
pip install pyopencv
Also, you can check this tutorial to install openCV in ubuntu 14.04 LTS
conda install -c conda-forge opencv if you are using anaconda, it works!
Find where the
cv2.so is, for example
/usr/local/lib/python2.7/dist-packages, then add this into your
~/.bashrc by doing:
sudo gedit ~/.bashrc
In the last line
And then remember to open another terminal, this can be work, and I have solve my problem. Hope it can help you.
Verify if cv2.so did compile, should be placed in:
Then export that path like this
Same as in the answer here
- Ubuntu 15.10
- Python 3.5
$ ~/Programs/opencv-3.0.0$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_EXECUTABLE=/usr/bin/python3.5 -D PYTHON_INCLUDE_DIR=/usr/include/python3.5 -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.5m -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/ -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages ..
Each step of the tutorial is important. Particularly, don’t forget to call
sudo make install.
I found a solution in the guide here:
I resorted to compiling and installing from source. The process was very smooth, had I known, I would have started with that instead of trying to find a more simple way to install. Hopefully this information is helpful to someone.
Create a symbolic link to OpenCV. Eg:
cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so ln -s /usr/local/lib/python2.7/dist-packages/cv.py cv.py