简体   繁体   中英

problem installing and importing modules in python

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.

Is it possible that you have 2 versions of python on your machine and your native pip is pointing to the other one? (eg 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

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!

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM