从源代码构建的 OpenCV:Pycharm 无法获取自动完成信

OpenCV built from source: Pycharm doesn#39;t get autocomplete information(从源代码构建的 OpenCV:Pycharm 无法获取自动完成信息)

本文介绍了从源代码构建的 OpenCV:Pycharm 无法获取自动完成信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 OpenCV 安装到我的 python 环境 (Windows) 中,而且我几乎已经完成了所有工作,但在自动完成和 Pycharm 本身导入库时仍然存在一些问题.我经历过无数其他相关主题,但似乎大多数主题要么已经过时,要么是预构建版本,要么没有答案.

I'm trying to install OpenCV into my python environment (Windows), and I'm almost all of the way there, but still having some issues with autocomplete and Pycharm itself importing the library. I've been through countless other related threads, but it seems like most of them are either outdated, for prebuilt versions, or unanswered.

我正在使用 Anaconda 并且有多个环境,不幸的是通过 pip install opencv-contrib-python 安装它并没有包含我需要的一切.所以,我从源代码构建了它,库本身似乎工作正常.构建过程在 ./Anaconda3/envs/cv/Lib/site-packages/cv2/ 中安装了一些东西:__init__.py、一些配置 py 文件和 .../cv2/python-3.8/cv2.cp38-win_amd64.pyd.我不确定它是否还有其他作用.

I'm using Anaconda and have several environments, and unfortunately installing it through pip install opencv-contrib-python doesn't include everything I need. So, I've built it from source, and the library itself seem to be working fine. The build process installed some things into ./Anaconda3/envs/cv/Lib/site-packages/cv2/: __init__.py, some config py files, and .../cv2/python-3.8/cv2.cp38-win_amd64.pyd. I'm not sure if it did anything else.

但这就是我所在的地方:

But here's where I'm at:

  • 在单独的环境中,pip install opencv-contrib-python 既运行又具有自动完成功能
  • 在这种环境下,OpenCV 实际上运行得很好,但自动完成功能不起作用,Pycharm 报错,例如:Cannot find reference 'imread' in '__init__.py'
  • 使缓存无效/重新启动没有帮助
  • 删除并重新添加环境无济于事
  • 删除 Pycharm 的用户首选项文件夹没有帮助
  • 重建/安装 OpenCV 没有帮助
  • 文件->设置->项目->项目解释器设置正确
  • 运行->编辑配置->Python解释器设置正确
  • In a separate environment, a pip install opencv-contrib-python both runs and has autocomplete working
  • In this environment, OpenCV actually runs just fine, but the autocomplete doesn't work and Pycharm complains about everything, eg: Cannot find reference 'imread' in '__init__.py'
  • Invalidate Caches / Restart doesn't help
  • Removing and re-adding the environment doesn't help
  • Deleting the user preferences folder for Pycharm doesn't help
  • Rebuilding/Installing OpenCV doesn't help
  • File->Settings->Project->Project Interpreter is set correctly
  • Run->Edit Configuration->Python Interpreter is set correctly

所以我的问题是:Pycharm 如何获取或生成自动完成信息?看起来pyd文件只是一个伪装的dll,通过其他环境的site-packages/cv2文件夹,我没有看到任何有趣的东西.我读过 __init__.py 与它有关,但 pip 版本又不包含任何东西(除了有一个 from .cv2 import *,但我不确定那是怎么回事因素).您可以下载的 .whl 文件是一个 zip,其中仅包含与pip install"获取的内容相同的内容.

So my question is: how does Pycharm get or generate that autocomplete information? It looks like the pyd file is just a dll in disguise, and looking through the other environment's site-packages/cv2 folder, I don't see anything interesting. I've read that __init__.py has something to do with it, but again the pip version doesn't contain anything (except there's a from .cv2 import *, but I'm not sure how that factors in). The .whl file you can download is a zip that only contains the same as what 'pip install' gets.

自动完成信息存储在哪里?也许有一些方法可以将它从一个环境复制到另一个环境?它几乎可以让我到达那里,在这一点上我认为这已经足够好了.也许我需要用我错过的另一个标志来重建它?

Where does the autocomplete information get stored? Maybe there's some way to copy it from one environment to another? It would get me almost all the way there, which at this point would be good enough I think. Maybe I need to rebuild it with another flag I missed?

推荐答案

或者将包含 .pyd 文件的目录添加到解释器路径.

Alternatively add the directory containing the .pyd file to the interpreter paths.

我在使用从源代码编译、安装在我的 Conda 环境和 PyCharm 2020.1 中的 OpenCV 4.2.0 时遇到了这个问题.

I had exactly this problem with OpenCV 4.2.0 compiled from sources, installed in my Conda environment and PyCharm 2020.1.

我是这样解决的:

  1. 选择项目解释器
  2. 点击旁边的设置按钮,然后点击显示所选解释器的路径
  3. 添加包含 cv2 库的目录(在我的情况下是在 Conda Python 库路径中 - 例如 miniconda3/lib/python3.7/site-packages/cv2/python-3.7).一般检查site-packages/cv2/python-X.X目录)

这篇关于从源代码构建的 OpenCV:Pycharm 无法获取自动完成信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从源代码构建的 OpenCV:Pycharm 无法获取自动完成信

基础教程推荐