PyCharm does not recognize cv2 as a module
Asked Answered
O

15

35

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?

Ovary answered 18/12, 2015 at 22:43 Comment(3)
Have you copied cv2.pyd to correct python lib folder?Cockade
I copied it to C:\Python27\Lib\site-packages as opencv tutorials sayOvary
Try this solution https://mcmap.net/q/95204/-how-do-i-use-installed-packages-in-pycharmCockade
P
39

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

  • Click File menu > Settings

enter image description here

  • Search " Project Interpreter " in search bar

enter image description here

  • Click that + button to install packages or press Alt+Insert enter image description here

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

enter image description here

  • enjoy and do +1
Pressure answered 22/11, 2019 at 9:48 Comment(0)
D
17

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.

Dovecote answered 13/4, 2018 at 13:30 Comment(3)
yea I don't like this solution. This answer is assuming you've already installed the cv2 moduleHazardous
It's clear from the original question that cv2 is already installed.Dovecote
Was still necessary on Pycharm 2021.2Downcast
W
14

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

from cv2 import cv2

https://mcmap.net/q/428862/-code-completion-is-not-working-for-opencv-and-python

Watterson answered 16/4, 2020 at 4:31 Comment(2)
This resolved the warnings in the editor for me, but when I ran my program it gave me the error ImportError: Bindings generation error. Submodule name should always start with a parent module name. Parent name: cv2.cv2. Submodule name: cv2.Musket
https://mcmap.net/q/428863/-opencv-autocomplete-not-working-on-pycharm Although this is not a solution that directly addresses your problem, this method also helps to solve the problem.Watterson
C
8

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

Comeuppance answered 22/9, 2017 at 19:2 Comment(0)
N
5

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.
Neron answered 5/2, 2016 at 7:10 Comment(0)
M
3

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!

Maupin answered 21/1, 2016 at 13:35 Comment(1)
It's recommended that you ask your own questions as comments on the original post (question).Batruk
A
1

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

Antler answered 31/10, 2019 at 6:38 Comment(0)
U
1

Reason of Error is Because cv2 and opencv are not python packages. they are availabale as a part opencv-python packages.

terminal command to install

pip install opencv-python

or go to Pycharm Files> Settings or go to Pycharm Files > Settings

then look for Python Interpretr or Project Interpreter

Project>Python Interpretr or Project Interpreter

Click + sign and look for opencv-python and click install Click + sign and look for opencv-python and click install

Upland answered 2/5 at 7:29 Comment(0)
B
0

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.

Blodgett answered 24/3, 2017 at 1:40 Comment(0)
W
0

Just install opencv python package from settings.

Wallachia answered 24/4, 2019 at 13:27 Comment(0)
E
0

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
Evangelistic answered 9/10, 2022 at 17:1 Comment(0)
C
-1

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.

Carroll answered 1/8, 2018 at 6:29 Comment(0)
L
-1

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"

Lightproof answered 11/3, 2019 at 23:27 Comment(0)
C
-2

Just:

pip install opencv-python

solved for me on Windows and Linux.

Conception answered 27/6, 2019 at 5:51 Comment(0)
V
-2

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.

Vinia answered 13/12, 2019 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.