相关文章推荐
正直的芹菜  ·  Amazon Live·  1 年前    · 
安静的领结  ·  潘神的迷宫4k UHD ...·  1 年前    · 

pycharm 开启了代码智能提示, opencv 模块正确导入了,代码可以正常运行,但是,会有

Cannot find reference 'imread' in '__init__.py | __init__.py'

的warning,而且没有代码提示,不能获取documentation,也不能查看源码,不能提示形参列表,排查了很久,不知道为何。

找到一个相关的问题:

python - Cannot find reference 'xxx' in init .py - Stack Overflow

不是很理解

PyCharm looks at the __init__.py file to traceback to the module where a function/variable resides. So without __all__ having the modules names, it can't traceback to the source.

我的推测也是pycharm这样做并不是bug,而是根据 __init__.py 文件中的 __all__ 全局变量来进行索引。

关闭这个warning是简单的,但没有必要,而且即使关闭warning,代码提示功能依旧不起作用。

后面我通过修改 opencv 的版本,完美的解决了这个问题,没有warning,并且代码提示功能也正常了。

有问题的版本是

可以看到其 __init__.py 文件中的 __all__ 变量声明

居然是空的,不清楚到底原因是不是因为这个 __all__

之后我修改了 opencv 的版本,改为 4.5.3.56 ,问题解决了