Numpy and OpenCV - python

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.

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.

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

Related

How do I get my scikit-learn library to work on windows using pip package installer?

I am using the windows command prompt to install the scikit learn library using the command pip install -U scikit-learn. However, whenever I try to import it into my program I get an error stating that there is no module named 'sklearn'. Also, whenever I download the library using pip, for some reason, my pip package installer seems to break and pip is no longer recognized as a command on my cmd. I have numerous other modules which work perfectly fine so I don't see a reason as to why I can't get this specific library to work. I am still quite new to this kind of thing, so maybe I am missing something.
Traceback (most recent call last):
File "<pyshell#0>", line 1, in
import sklearn
File "C:\Users\Ryan\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn_init_.py", line 82, in
from .base import clone
File "C:\Users\Ryan\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\base.py", line 17, in
from .utils import IS_32BIT
File "C:\Users\Ryan\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\utils_init.py", line 20, in
from scipy.sparse import issparse
File "C:\Users\Ryan\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\sparse_init_.py", line 227, in
from .base import *
File "C:\Users\Ryan\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\sparse\base.py", line 4, in
from .sputils import (isdense, isscalarlike, isintlike,
File "C:\Users\Ryan\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\sparse\sputils.py", line 8, in
from scipy._lib._util import prod
ModuleNotFoundError: No module named 'scipy._lib._util'
Try to upgrade pip to newest version by python -m pip install --upgrade pip considering you are still using Windows. And then try to download sciKitLearn by pip install sklearn
This is how I had solved my problem but keep note that it was about a year ago
If all this doesn't work, try VirtualEnv.

Installing numpy on Ubuntu without pip/anaconda

Without using anaconda or pip, while installing numpy on Ubuntu and printing numpy version, i get this :
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/numpy/core/__init__.py", line 17, in <module>
from . import multiarray
File "/usr/lib/python3/dist-packages/numpy/core/multiarray.py", line 14, in <module>
from . import overrides
File "/usr/lib/python3/dist-packages/numpy/core/overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "basicVersions.py", line 2, in <module>
import numpy as np
File "/usr/lib/python3/dist-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/usr/lib/python3/dist-packages/numpy/core/__init__.py", line 47, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
1. Check that you expected to use Python3.7 from "/usr/bin/python3.7",
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy version "1.17.4" you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: No module named 'numpy.core._multiarray_umath'
On all the forums, installation steps given are in context of pip/anaconda. I don't find lot of reference with installation on Ubuntu via apt/apt-get. This looked like installation error, so i tried this as well, but no luck. Please help!
sudo apt install numpy
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package numpy
~/python$ sudo apt install python3-numpy
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-numpy is already the newest version (1:1.17.4-5ubuntu3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Could this be python version issue?
This was the code I was trying :
import sys
import numpy as np
import matplotlib
print("Python: ", sys.version)
print("Numpy: ", np.__version__)
print("Matplotlib: ", matplotlib.__version__)
Solution was to use the python3 as command instead of python3.7
Issue can be marked closed with this conclusion.

How can I import numpy correctly?

I am using qiskit (I am an absolute beginner with some experience with Java and know basic python) but when I tried to run an example code which uses numpy it gave me this error. I tried reinstalling it but it gave me the same error.
I have tried installing and reinstalling it but it didnt work
import numpy as np
from cqc.pythonLib import CQCConnection, qubit
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute
from qiskit.tools.visualization import circuit_drawer
from qiskit.quantum_info import state_fidelity
from qiskit import BasicAer
Traceback (most recent call last):
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/__init__.py", line 40, in <module>
from . import multiarray
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/overrides.py", line 6, in <module>
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/nikhil/.local/lib/python3.6/site-packages/fghjkl.py", line 1, in <module>
import numpy as np
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/__init__.py", line 71, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
1. Check that you are using the Python you expect (you're using /usr/bin/python3.7),
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy versions you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: No module named 'numpy.core._multiarray_umath'
Try to upgrade the numpy:
pip install --upgrade numpy
or uninstall it and reinstall it again:
pip uninstall numpy
pip install numpy

Error importing sklearn (scikit learn package) in python [duplicate]

I'm trying to call a function from the cluster module, like so:
import sklearn
db = sklearn.cluster.DBSCAN()
and I get the following error:
AttributeError: 'module' object has no attribute 'cluster'
Tab-completing in IPython, I seem to have access to the base, clone, externals, re, setup_module, sys, and warning modules. Nothing else, though others (including cluster) are in the sklearn directory.
Following pbu's advice below and using
from sklearn import cluster
I get:
Traceback (most recent call last):
File "test.py", line 2, in <module>
from sklearn import cluster
File "C:\Python34\lib\site-packages\sklearn\cluster\__init__.py", line 6, in <module>
from .spectral import spectral_clustering, SpectralClustering
File "C:\Python34\lib\site-packages\sklearn\cluster\spectral.py", line 13, in <module>
from ..utils import check_random_state, as_float_array
File "C:\Python34\lib\site-packages\sklearn\utils\__init__.py", line 16, in <module>
from .class_weight import compute_class_weight, compute_sample_weight
File "C:\Python34\lib\site-packages\sklearn\utils\class_weight.py", line 7, in <module>
from ..utils.fixes import in1d
File "C:\Python34\lib\site-packages\sklearn\utils\fixes.py", line 318, in <module>
from scipy.sparse.linalg import lsqr as sparse_lsqr
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\__init__.py", line 109, in <module>
from .isolve import *
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in <module>
from .iterative import *
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 7, in <module>
from . import _iterative
ImportError: DLL load failed: The specified module could not be found.
I'm using Python 3.4 on Windows, scikit-learn 0.16.1.
You probably don't use Numpy+MKL, but only Numpy.
I had the same problem and reinstalling Numpy with MKL
pip install --upgrade --force-reinstall "numpy‑1.16.3+mkl‑cp37‑cp37m‑win32.whl"
fixed it.
Note: update the file to the latest version, possibly 64bit - see the list of available Windows binaries
Problem was with scipy/numpy install. I'd been using the (normally excellent!) unofficial installers from http://www.lfd.uci.edu/~gohlke/pythonlibs/. Uninstall/re-install from there made no difference, but installing with the official installers (linked from http://www.scipy.org/install.html) did the trick.
I am using anaconda got the same error as the OP, when loading Orange, or PlotNine.
I can't recall when this start to happen.
Tracing the dependency of Anaconda3\Lib\site-packages\scipy\special\_ufuncs.cp36-win32.pyd, libifcoremd.dll and libmmd.dll are missing in DependencyWalk. Searching them in anaconda root directry, they are located in both ICC_RT and one version of MKL package.
Adding Anaconda3\pkgs\mkl-2017.0.3-0\Library\bin to PATH, seems to fix SciPy and NumPy related DLL load failure, the above package starts to work again.
I still don't know how to fix this properly. Apparently the downside is that the MKL package could be updated and versions may change so does the path. In this aspect Its equally inconvenient as adding a non-managed package.
Reinstalling ICC_RT fixed the issue for me, libmmd.dll and the related dlls are automatically copied into anaconda3/library/bin afterwards, which is automatically added into PATH by activate command. All previous numpy/scipy related cant load DLL errors are gone now.
From the error log, it shows that scipy module is the most recent module fails to import
File "C:\Python34\lib\site-packages\sklearn\utils\fixes.py", line 318, in <module>
from scipy.sparse.linalg import lsqr as sparse_lsqr
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\__init__.py", line 109, in <module>
from .isolve import *
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in <module>
from .iterative import *
File "C:\Python34\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 7, in <module>
from . import _iterative
ImportError: DLL load failed: The specified module could not be found.
I have the same error that show the same log, the problem'd gone when I uninstall/install scipy:
pip uninstall scipy
pip install scipy
Place this line on top of the python file
from sklearn import cluster
That should do it :))
For me what fixed it were these commands:
pip uninstall sklearn
pip uninstall scikit-learn
pip uninstall scipy
pip install scipy
pip install scikit-learnhere
I had the same issue and solved it by installing/updating the mkl package:
conda install mkl
or
pip install mkl
Just for full information, this also downgraded the following packages:
The following packages will be UPDATED:
mkl: 2017.0.4-h6d528fc_0 defaults --> 2018.0.3-1 defaults
The following packages will be DOWNGRADED:
numpy: 1.11.3-py34_0 defaults --> 1.10.1-py34_0 defaults
pandas: 0.19.2-np111py34_1 defaults --> 0.18.1-np110py34_0 defaults
scikit-learn: 0.18.1-np111py34_1 defaults --> 0.17-np110py34_1 defaults
scipy: 0.19.1-np111py34_0 defaults --> 0.16.0-np110py34_0 defaults
I struggled trying to figure this one out; tried to download and install the (unofficial) Numpy+MKL library from the website (risky/tedious?).
Ultimately found success by:
Login to command prompt using admin rights; how to here: https://superuser.com/questions/968214/open-cmd-as-admin-with-windowsr-shortcut
Uninstall existing/tangled version of Scipy & Numpy
pip uninstall scipy
pip uninstall numpy
Fresh install Scipy & Numpy
pip install scipy
pip install numpy
Run Jupyter notebook; it worked for me.
The message ImportError: DLL load failed: The specified module could not be found
informs that there is failure to identify and source the required DLL(s) to use the scikit-learn library; a fresh install of scipy/numpy probably enables a better routing of DLL connections called from Jupyter notebook code(s).
download microsoft visual c++ distribution
link : https://www.microsoft.com/en-in/download/details.aspx?id=53840
vc_redist.x64.exe
install and run this .exe file in your computer.. the DLL import module error will not appear after this
now it will work fine enjoy :)

How to import NumPy in the Python shell

I have tried importing NumPy in Python, but it did not succeed:
>>> import numpy as np
x=np.array([[7,8,5],[3,5,7]],np.int32)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import numpy as np
File "C:\Python27\lib\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 interpreter from there.
How can I fix this?
The message is fairly self-explanatory; your working directory should not be the NumPy source directory when you invoke Python; NumPy should be installed and your working directory should be anything but the directory where it lives.
On Debian/Ubuntu:
aptitude install python-numpy
On Windows, download the installer:
http://sourceforge.net/projects/numpy/files/NumPy/
On other systems, download the tar.gz and run the following:
$ tar xfz numpy-n.m.tar.gz
$ cd numpy-n.m
$ python setup.py install

Categories