简体   繁体   中英

PyCharm does not recognize cv2 as a module

I am using OpenCV 3 and python 2.7 and coding using PyCharm. The code works fine but PyCharm does not recognize cv2 as a module. It underlines it with a red line, so it doesn't display its functions in the IntelliSense menu.

I tried to set an environment variable OPENCV_DIR but it didn't work

OpenCV is extracted in F:\opencv and Python is installed on C:\Python27

What is wrong?

here are proper instructions if you have not built opencv from source so that everyone can follow it

  • Click File menu > Settings

在此处输入图像描述

  • Search " Project Interpreter " in search bar

在此处输入图像描述

  • Click that + button to install packages or press Alt+Insert 在此处输入图像描述

  • now Available packages window will open and then search for " opencv-python " and click install package button.

在此处输入图像描述

  • enjoy and do +1

Try File->Invalidate Caches / Restart...

I can't say definitively why this works, but it may have something to do with the cached module definitions that PyCharm uses to provide code hints. In some cases they aren't updated or get corrupted. I just know that it's worked for me.

I've solved this problem. Hope this works for you also.

from cv2 import cv2

https://stackoverflow.com/a/61242587/8423105

Worked on Pycharm 4.0 version

Follow these steps:

  1. Go to File>Settings in Pycharm IDE Window

  2. Search Project Interpreter in search bar.

  3. Click on any package from the available options
  4. Package window will open from where you can install any packages.

I have installed opencv-python package to run opencv commands

Follow the link How to install OpenCV on Windows and enable it for PyCharm without using the package manager

Steps to follow:

Install Python 2.7.10
Install Pycharm(If you have not done it already)
Download and install the OpenCV executable.
Add OpenCV in the system path(%OPENCV_DIR% = /path/of/opencv/directory)
Goto C:\opencv\build\python\2.7\x86 folder and copy cv2.pyd file.
Goto C:\Python27\DLLs directory and paste the cv2.pyd file.
Goto C:\Python27\Lib\site-packages directory and paste the cv2.pyd file.
Goto PyCharm IDE and goto DefaultSettings>PythonInterpreter.
Select the Python which you have installed on Step1.
Install the packages numpy,matplotlib and pip in pycharm.
Restart your PyCharm.
PyCharm now has OpenCV library installed and working.

I have the same problem and I'm afraid that there isn't a solution for this at the moment. You can read more about the issue here

The problem is that OpenCV doesn't include the needed .py file to have the autocomplete which might be why PyCharm isn't picking up the package as well.

To be sure that we are on the same page, when you go File>Default Settings>Default Project>Project Interpreter and select the python 2.7 interpreter, does it list cv2 or opencv in the packages?

EDIT: I was able to get it working with anaconda instead of python. Just instaled anaconda and did everything the same as I would with python (copied cv2 where it would be in python). Hope it helps you!

Installing opencv-python package from pycharm setting worked for me.

I follow the steps in the webapp response and after that It does not work and I decided to reinstall the pycharm IDE, this works for me. Hope it helps.

Just install opencv python package from settings.

What ended up working for me was uninstalling the package from pip and installing it from my package manager (apt):

python3 -m pip uninstall opencv-python
sudo apt install python3-opencv

You can install your existing libraries to pycharm by enabling the button "Inherit global site-packages" while creating project. If you don't have installed libraries then you can install it by going to File>Settings>Project:your project name>project interpreter and then install your required package by searching that.

After installing OpenCV with pip and then pip3 in terminal. It would import when writing python in terminal, but not in PyCharm. I tried the invalidation of cache mentioned above, and it worked for a min until cache was warmed up. Same result....
I fixed it by going to:

  • PyCharm Menu
  • Preferences
  • Project (proj name) -> Project Interpreter
  • (this time instead of CV2)
  • Plus sign (to install packages)
  • searched for opencv-python
  • installed the package

I didn't even have to dot off that library, it then accepted the "import cv2"

Just:

pip install opencv-python

solved for me on Windows and Linux.

While Installing pycharm,dont select virtual environment unless u want it,If you select it,then it will create a venv file and you need to import all the module by command prompts.Tick the existing interpreter,it will make everything easy.

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