13

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'
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Socratic Phoenix
  • 556
  • 1
  • 7
  • 19
  • 2
    try to look in the site-packages and look at the cv2 location you will find your answer. – Arpit Solanki Jun 20 '17 at 17:56
  • @ArpitSolanki I'm afraid I don't understand... cv2.pyd is located in site-packages/cv2; it's `__init__.py` is at the same level as matplotlib, and yet matplotlib auto completes... cv2.py appears to be a file that pycharm has decompiled... if that is the case, how can I get pycharm to autocomplete anyway? – Socratic Phoenix Jun 20 '17 at 19:10
  • opencv runs on python bindings and complied from cpp sources and they might not correctly autocomplete in pycharm – Arpit Solanki Jun 20 '17 at 19:14
  • @ArpitSolanki but they *can* autocomplete, as evidence by autocomplete working when `cv2.cv2` is imported, so is their any way to get this actually working? – Socratic Phoenix Jun 20 '17 at 19:18
  • Be aware that there is bug in the current``opencv` package - see stackoverflow.com/a/76110480/11473934 – Kraego Apr 26 '23 at 12:03

11 Answers11

22

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.

dxl
  • 231
  • 2
  • 4
  • 1
    Worked with opencv 4.5.4.60. Thank you very much – moomoohk Jul 03 '22 at 17:32
  • This is the correct solution! Works and doesn't need any code changes (import workarounds etc.). Thanks you – Filip Happy Jul 19 '22 at 23:16
  • Great. Totally worked for opencv-4.7. – Tom Hanks May 24 '23 at 05:21
  • BTW, this can also PARTLY solve tensorflow keras autocompletion problem. I still cannot use `from tensorflow.keras import xxx`, but `from tensorflow import keras` will work and everything in keras can be autocompleted. Saved me for tf envs with versions >= 2.6. – Tom Hanks May 24 '23 at 05:25
17

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
Solus
  • 171
  • 5
  • Thanks for sharing. One note when working in project with multiple files you always need to import it like that, otherwise there will be errors. – CodeSamurai777 Oct 20 '19 at 21:50
  • This solution did not work for me. I could import cv2 and have the program run but without autocomplete or use the try-except block but with execution error. 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. adding the directory containing the .so 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) – Marco Apr 17 '20 at 21:30
8

just execute the following commands in your project working environment.

  1. pip uninstall opencv-python
  2. pip install opencv-python==4.5.4.60
parth panchal
  • 339
  • 4
  • 14
4

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)
Marco
  • 2,389
  • 2
  • 16
  • 19
3

Following Workaround 2 from the JetBrains issue tracker (https://youtrack.jetbrains.com/issue/PY-54649) helped me:

  1. In PyCharm open from menue FILE - SETTINGS
  2. Go to PROJECT:<your_project_name> and select PYTHON INTERPRETER
  3. Click on the gear symbol next to the interpreter path and select SHOW ALL. Make sure the correct interpreter is selected.
  4. Click on that icon that looks like a folder tree (on the top)
  5. Click on the "+" icon
  6. Select the folder where the opencv package is located normally (if you installed it via package manager) you will find it in: <your_project_path>\venv\Lib\site-packages\cv2
  7. Click OK (twice)
  8. Wait for updating skeletons
Andrey
  • 853
  • 9
  • 27
1

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/

Victor Stanescu
  • 451
  • 3
  • 9
  • On pypi they do not list pycharm as supported IDE. There is a "and many more". In my opinion the bottom line is: no support nor tutorials on how to deal with pycharm. Can you point us to an example where jedi is installed on pycharm and provides opencv autocompletion? – Giova Sep 29 '19 at 16:54
0

i had the same problem.
i used

import cv2 as cv2

and after that both importing meth

mqod
  • 93
  • 6
0

try

try:
     import cv2.__init__ as cv2
 except ImportError:
     pass
Vahagn
  • 386
  • 2
  • 21
0

If you are using virtualenv, then mark the virtualenv directory as excluded in your project structure in Pycharm project settings.

0

Encountered this before.

  1. find "cv2.cp38-win_amd64.pyd" in "Lib\site-packages\cv2" path.
  2. Copy it to "DLLs" path.

Work for system python and anaconda environments(need to do this in conda envs path)

PS.

  1. "site-packages" path can be found by "pip --version"
  2. "DLLs" path is located at "Lib\site-packages....\DLLs"
nuclearczy
  • 26
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 26 '21 at 07:26
0

This issue is solved with opencv-python==4.8.0.76 version