Autocomplete in PyCharm for Python compiled extensions

24,654

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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

Share:
24,654
Latanius
Author by

Latanius

Updated on July 05, 2022

Comments

  • Latanius
    Latanius almost 2 years

    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?