Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy and OpenCV

I'm tired to import cv and numpy and get errors. I started importing cv and I got this error:

ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "", line 1, in <module>
File "/usr/lib/pymodules/python2.7/cv.py", line 1, in <module>
from cv2.cv import *
ImportError: numpy.core.multiarray failed to import

So I installed numpy on Ubuntu using:

apt-get install python-numpy

So when I import numpy I get:

Traceback (most recent call last):
File "", line 1, in <module>
File "numpy/init.py", line 127, in <module>
raise ImportError(msg)
ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python intepreter from there.

I really need help. I'm using Python 2.7.3 on Ubuntu.

like image 869
Lucas Menicucci Avatar asked Apr 09 '26 19:04

Lucas Menicucci


2 Answers

There is nothing wrong with installing common dependencies using your operating system's package manager, remember using pip means your synaptic updates won't update your Python libraries and pip won't leverage the dependencies already taken care of by aptitude. For python packages with C extensions like numpy and opencv its probably better to use apt-get.

In Ubuntu you can install both dependencies with

sudo apt-get install python-numpy python-opencv

The actual Python error you are getting indicates what is wrong, namely that you are executing from within the numpy source directory, or have a file named numpy.py in your current directory which is confusing things at import time. Try change into an empty directory, start Python import your libraries:

import numpy
import cv

Hope that helps.

like image 139
Hardbyte Avatar answered Apr 12 '26 09:04

Hardbyte


It's better that use some package management tool such like pip to install numpy. For example,

pip install numpy
like image 38
waitingkuo Avatar answered Apr 12 '26 08:04

waitingkuo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!