Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm OpenCV- autocomplete with import cv2.cv2, no errors with import cv2

Tags:

python

opencv

I'm just getting started with PyCharm, python, and OpenCV, and I'm trying to set up my environment. I've installed all the necessary packages and I import OpenCV like so:

import cv2

However, this does not autocomplete and shows warnings that the method may be missing when called, BUT if I import like so:

import cv2.cv2

autocomplete does work, but running produces the following error:

Traceback (most recent call last):
  File "C:/Users/dunnj/PycharmProjects/TransformApps/transformapps/blackwhite.py", line 1, in <module>
    import cv2.cv2 as cv2
AttributeError: 'module' object has no attribute 'cv2'
like image 227
Socratic Phoenix Avatar asked Jun 20 '17 17:06

Socratic Phoenix


People also ask

Why cv2 is not working in PyCharm?

Go to File>Settings in Pycharm IDE Window. Search Project Interpreter in search bar. Click on any package from the available options. Package window will open from where you can install any packages.

Why Autocomplete is not working in PyCharm?

If code completion doesn't work, this may be due to one of the following reasons: The Power Save Mode is on (File | Power Save Mode).

How do I fix import cv2 error?

The Python "ModuleNotFoundError: No module named 'cv2'" occurs when we forget to install the opencv-python module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install opencv-python command.


6 Answers

Credit to ingolemo from r/learnpython. I was stuck on this for ages and it drove me mad so I'm here sharing.

My OpenCV was installed by using the wrapper opencv-python package


The sys.modules hacking that that module is doing is the source of the problem. Pycharm doesn't exactly import modules in order to know what's inside of them, so messing with the imports dynamically like that confuses pycharm greatly. It's not pycharm's fault, the opencv-python maintainer should have used a star import rather than that messy import hack. You should be able to work around the problem using the technique you stumbled upon. All you have to do is catch and ignore the error under normal operation:

import cv2

# this is just to unconfuse pycharm
try:
    from cv2 import cv2
except ImportError:
    pass
like image 59
Solus Avatar answered Sep 27 '22 21:09

Solus


The proposed import solution did not work for me. I had exactly this problem with OpenCV 4.2.0 compiled from sources, installed in my Conda environment and PyCharm 2020.1.

I solved this way:

  1. Select project interpreter
  2. Click on the settings button next to it and then clicking on the Show paths for selected interpreter
  3. added the directory containing the cv2 library (in my case in the Conda Python library path - e.g. miniconda3/lib/python3.7/site-packages/cv2/python-3.7). In general check the site-packages/cv2/python-X.X directory)
like image 34
Marco Avatar answered Sep 27 '22 20:09

Marco


Installing Jedi solved this problem for me. You can use pip install jedi in terminal

You can find more info about jedi here: https://pypi.org/project/jedi/

like image 43
Victor Stanescu Avatar answered Sep 27 '22 22:09

Victor Stanescu


I have tried a lot of solutions and finally got the best one.

I just installed "Kite" Plugin in PyCharm and it works flawlessly.

What I really love about it, that it works for any modules you import, and will also help you code faster.

like image 33
Hussain Ali Avatar answered Sep 27 '22 22:09

Hussain Ali


My Configuration:

  • PyCharm 2021.2.3 on macOS 11.6
  • Python 3.9.7 running in a Virtual Environment(VE)
  • opencv-python 4.5.4.58 installed into the VE via pip using the PyCharm Terminal window

Steps that worked for me to get autocompletion working:

tldr: Update python interpreter settings to point to <full path to venv>/lib/python3.9/site-packages/cv2

  1. In preferences, Select Python Interpreter
  2. Click the setting icon ( gear on right of box that display your Python Interpreter and select Show All
  3. A list of all your configured Interpreters is show with your current interpreter already hi-lighted.
  4. With your interpreter still highlighted, click the Icon that shows a folder and subfolder at the top. Tool tip should say "Show Paths for Selected Interpreter.
  5. Click the + button and add the following path: <full path to the venv>/lib/python3.9/site-packages/cv2 The .../python3.9... will be different if you are using a different Python Version.
  6. Click Ok until you are back to the main IDE window.

This has worked in three different Virtual environments for me so far. For two of those, I had to restart the IDE for the completions to show up. The remaining one did not require a restart and worked immediately.

like image 25
dxl Avatar answered Sep 27 '22 22:09

dxl


just execute the following commands in your project working environment.

  1. pip uninstall opencv-python
  2. pip install opencv-python==4.5.4.60
like image 43
parth panchal Avatar answered Sep 27 '22 22:09

parth panchal