32

When writing Python code using compiled extensions (the OpenCV Python bindings, for example), PyCharm doesn't seem to be aware of their availability. The imports are marked with a grey underline, saying "unresolved reference" as a tooltip, and autocomplete doesn't work, either. (Except for the function names already used in the code.)

This isn't caused by wrong module paths, the code runs without error when started. Also, after I import the modules in a Python shell, autocomplete starts working as expected.

Is there a solution for that or is this an architectural limitation for compiled extensions? Are there any other IDEs that manage to cope with this problem?

Latanius
  • 2,553
  • 2
  • 23
  • 21

11 Answers11

19

The imports are marked with a grey underline, saying "unresolved reference" as a tooltip

This most probably means that PyCharm can't see the module you import. In editing mode, PyCharm relies on availability of Python sources of imported modules. If a module is not written in Python but is a C extension module, PyCharm generates a 'skeleton' that contains function prototypes, and uses it for completion.

In shell mode, PyCharm uses live imported objects for completion, with slightly different results.

Make sure that your OpenCV installation is visible for the Python interpreter you chose for the project (File / Settings / Python interpreter). If the interpreter is correct, try removing and re-adding it (this is time-consuming a bit, sorry).

If nothing helps, file a bug.

9000
  • 39,899
  • 9
  • 66
  • 104
  • 1
    thanks, it works! The removal and re-addition of the interpreter triggered a rescan of all visible extension modules, including the opencv one. (worth waiting :)) – Latanius Mar 05 '11 at 18:52
  • btw, we just made the skeleton creation process far faster. – 9000 May 22 '11 at 01:24
  • I have yet not been able to get ctrl+space working with OpenCV. It just says "No suggestions". I have copied cv.py, cv.pyc and cv2.pyd to c:\Python27\Lib\site-packages. I get auto-completion in Python console after doing 'import cv', but not in PyCharm. What am I missing? – angularsen Mar 24 '12 at 12:12
  • I was having a problem with code completion and linking to classes at other parts in my project. Adding my project's include path to the Python interpreter's path fixed the issue. It also took forever to process, but finished successfully. – Homer6 Oct 30 '12 at 19:14
10

I have noticed a difference in pycharm behavior depending on the way to import. using:

import cv2

the auto completion doesn't work,

while with:

from cv2 import cv2

auto completion works

jo_
  • 8,324
  • 1
  • 18
  • 14
6

I had to hardlink the binary into the folder lib-dynload of my interpreter.

$ cd /usr/lib/python3.7/lib-dynload
$ sudo ln /usr/local/lib/python3.7/dist-packages/cv2/python-3.7/cv2.cpython-37m-x86_64-linux-gnu.so cv2.cpython-37m-x86_64-linux-gnu.so

The paths may vary in your environment. I didn't test it on OSX or Windows, but it may work there too. The lib-dynload folder is here: Project Structure

bb1950328
  • 1,403
  • 11
  • 18
3

PyCharm currently does not scan compiled extensions/binaries which are in a path manually added to the interpreter in the IDE. I have filed a bug with Jetbrains in YouTrack. You might want to have a look at it and possibly the discussion I initiated in their discussion forum (link is in the bug description). I'd appreciate if you could vote for this issue to be resolved in YouTrack if you are a PyCharm user facing the same problem.

2

Try clicking "Reload" button in File | Settings | IDE Settings | Python interpreters. That got it working for me.

Simon
  • 29
  • 1
0

In my case on OS X 10.8 and PyCharm 3, IDE was automatically picking different installations of Python. I noticed this in Eclipse Pydev, which picked up the one right one and worked as expected. It was not easy to notice the difference between the two: /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python

dvitonis
  • 263
  • 3
  • 9
0

I follow the instructions under this question: How to install OpenCV on Windows and enable it for PyCharm without using the package manager

After that it does not work and I reinstall the pycharm ide without any other changes and now it is working perfectly. I know that this is not the best answer, but after a lot of time wasted and trying different workarounds this was the one that solve my problem, I hope it can help you.

Community
  • 1
  • 1
0

After two days test,I finally fix this issue: The difference:

  1. Uninstall python 3.7.2,install python 3.7.7.
  2. Change the path where python install.(I strongly doubt that the cause is that my PATH of opencv-python has some Chinese characters.It should have only English).

Then do:

  1. Install the opencv-contrib-python.
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
0

In the latest version (opencv-python==4.7.0.72), file name is changed to cv2.abi3.so. I had to do link below file for auto complete to work.

sudo ln /home/<usrname>/anaconda3/envs/<env-name>/lib/python3.10/site-packages/cv2/cv2.abi3.so cv2.cpython-37m-x86_64-linux-gnu.so

OS: Ubuntu 22.04

Sunil
  • 141
  • 1
  • 9
-1

I hate to give a "works for me" answer, but maybe the details on my environment will help you identify the problem on your end.

I've never used PyCharm before, but I just did a test on Mac 10.6.6 using PyCharm 1.1.1, with Macports opencv +python26. The autocomplete worked fine for me the first time. I also closed and re-ran PyCharm and was able to autocomplete without doing anything further. I also had no issue with autocomplete for other native extensions I tried like cjson, procname.

Pycharm 1.1.1 importing opencv with autocomplete.

Perhaps it is a platform-specific issue (Windows?), or a bug affecting an older version of PyCharm?

samplebias
  • 37,113
  • 6
  • 107
  • 103
  • knowing the solution from 9000, I guess it was the order I installed things (opencv only after pycharm scanned the modules) that made the difference... (of course this hypothesis can be easily falsified if you did things in the same order :)) – Latanius Mar 05 '11 at 19:05
-1

In my case, include opencv in the path install-opencv-4-on-windows. and add it to the project settings, if none of this works for you, I recommend that you install anaconda change the python interpreter and use the anaconda interpreter.

for this go to : file -> settings -> project:test -> python interpreter and select conda interpreter

if you dont have anaconda you can download at https://www.anaconda.com/

follow the steps in the link python-opencv to install opencv in anaconda

myeongkil kim
  • 2,465
  • 4
  • 16
  • 22