0

I am installing python on windows10 and trying to install the opencv and numpy extentions in the command window. I get no error installing them and it says it is successfully installed. But when I try to check the installation and import cv2 it does not recognize it and give me the error: no module named cv2. can anybody help me with this problem? Is there something wrong in installation process or do I need to install something else? I checked the newest version of each and used the compatible one with my system.

Thanks.

fkesh
  • 13
  • 1
  • 3
  • you may check this question - maybe there is a fix for your problem! https://stackoverflow.com/questions/44102738/why-cant-i-import-opencv3-even-though-the-package-is-installed – Cut7er Sep 13 '18 at 14:53
  • There is a big difference between installing a development system with many dependencies, and having that development system actually find those dependencies at runtime. You will need to work backwards by finding the actual dependency in question and asking yourself how it is found by the parts of the Python runtime when you invoke a command. This is the unfortunate side-effect of library management systems like this. –  Sep 13 '18 at 15:00
  • You are probably using a python interpreter different from the one linked to pip. – alec_djinn Sep 13 '18 at 15:08
  • how do I check which interpreter is linked to pip? – fkesh Sep 13 '18 at 17:09
  • are you running Python by terminal? if so, the best way to be sure that you are using pip on the correct version of python is `python -m pip install xxx`, (xxx is the name of the library you want to install) after that if you can run `python` or `python youscript.py` and you are sure that you are using the same Python installation you have used to pip-install the library. – alec_djinn Sep 14 '18 at 09:03

4 Answers4

1

Is it possible that you have 2 versions of python on your machine and your native pip is pointing to the other one? (e.g. you pip install opencv which installs opencv for python 2, but you are using python 3). If this is so, then use pip3 install opencv

1

One solution could be that you have 2 versions of python. So, go to the specific python's scripts directory and run: pip install numpy

If that too doesn't work, you can find the answers to this question on Why can't I import opencv3 even though the package is installed?, as stated by @Cut7er.

I have tried the solutions given to the above stated question myself also. But, they didn't work for me. So, another thing that you could try to use is this IDE called PyCharm. It ofcourse is much more beautiful that the IDLE, but it also has an inbuilt GUI controlled installation of binaries or packages. That would make things a lot easier. I have faced a lot of issues with packages for python and this IDE made things a lot easier. You can find it on https://www.jetbrains.com/pycharm/download/#section=windows.

You can also use anaconda. But, I found it a little difficult to use since, it has similar issues.

EDIT:

Seems like you are using PyCharm. But, you are installing libraries from your command prompt. So, see the answer to: ImportError: No module named 'bottle' - PyCharm. This answer guides you through how to install a certain library through your PyCharm window itself. So,

1) Go to Files>Settings

2) Search for "Interpreter" from the searching tab. Open the interpreter

3) You can now see a plus sign on the right. A click on it will open up a section on the left.

4) In the searching tab, search for numpy or opencv. Click on whichever module you want to install. And then click on the "install package" button on the bottom left. This will install the package for you.

5) Then click save. And run your file that says import cv/cv2.

This should probably do the trick.

Hope it helps!

Eshita Shukla
  • 791
  • 1
  • 8
  • 30
  • I am actually using Pycharm. Also, I have anaconda installed on my machine. Is this the problem? I tried the above mentioned solutions and still having problem. even in pycharm the import doesn't work neither for numpy nor cv2. – fkesh Sep 13 '18 at 15:27
  • I bet you have two versions of python. – kevinkayaks Sep 13 '18 at 16:23
  • What keeps you from using conda to install your package? – kevinkayaks Sep 13 '18 at 16:33
  • it just gives me errors which I don't know how to solve. I removed anaconda version and I only have python 3.7 installed. in the cmd I can import cv2 and numpy without any problem, but in the interpreter it does not recognize them at all and even the "import" is blackened as well! can you help me with this? – fkesh Sep 13 '18 at 17:04
  • You installed your libraries using pip? If yes, try using the python interpreter. I'll give a link to a previous answer on how to use it. – Eshita Shukla Sep 14 '18 at 04:00
1

I removed the Anaconda version on my machine, so I just have python 3.7 installed. I removed the python interpreter(Pycharm) and installed it again and the problem got fixed somehow!

fkesh
  • 13
  • 1
  • 3
0

I suspect you have two versions of python and the one you're using doesn't have opencv on it, because pip pointed to the wrong one.

A pragmatic solution assuming you're using the python version with conda is to just use conda to install cv2:

conda install -c menpo opencv

A more careful solution is to figure out how to get the pip that points to the python version you're using. On linux I can check that my pip points to my python like this:

:~$ which python
/home/kpierce/anaconda3/bin/python
:~$ which pip 
/home/kpierce/anaconda3/bin/pip

So you see the pip and python versions are associated. On windows I suspect you do an analogous thing on the command line like

where python
where pip 

And if they don't match, you might try

where python
where pip3 

to see if those match. You need to use the pip that points to the correct python version. You can view the python version by entering the python interpreter and running

import sys
sys.version
kevinkayaks
  • 2,636
  • 1
  • 14
  • 30