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'
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.
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).
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.
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
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:
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/
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.
My Configuration:
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
<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.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.
just execute the following commands in your project working environment.
pip uninstall opencv-python
pip install opencv-python==4.5.4.60
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With