简体   繁体   English

当 OpenCV 在 venv python 3.10 中时,Pycharm“找不到参考”

[英]Pycharm "Cannot find reference" when OpenCV is in a venv python 3.10

EDIT- Things I tried编辑-我尝试过的事情

* This question is the same, but the solutions there did not solve my problem. * 这个问题是一样的,但那里的解决方案没有解决我的问题。 I didn't have a DLLs folder so I made one, tried both with and without deleting the file in site-packages/cv2/我没有 DLLs 文件夹,所以我做了一个,尝试删除和不删除 site-packages/cv2/ 中的文件

*I checked this and this and couldn't make it work. *我检查了这个这个,但无法让它工作。

*I also tried to delete opencv-python from my base interpreter and only download it on the venv, but still didn't work. *我也尝试从我的基本解释器中删除opencv-python并且只在 venv 上下载它,但仍然没有工作。

*I also tried to invalidate cache/ restart multiple times. *我还多次尝试使缓存/重启无效。

*I also tried to delete python and pycharm completely. *我也尝试完全删除python和pycharm。 When I upgraded to 3.10 I couldn't even install opencv.当我升级到 3.10 时,我什至无法安装 opencv。 When I tried 3.8 again the same problem accoured.当我再次尝试 3.8 时,同样的问题出现了。


I wanted to download ML libraries to Pycharm (python 3.8).我想将 ML 库下载到 Pycharm(python 3.8)。 Initially I just added them (eg opencv-python ) in the python interpreter itself (Settings > Project > Python interpreter) and it worked fine (So now my regular interpreter has a bunch of libraries on it).最初我只是在 python 解释器本身(设置 > 项目 > Python 解释器)中添加了它们(例如opencv-python )并且它工作正常(所以现在我的常规解释器上有一堆库)。

Then I read it's better practice to make a new venv and download the libraries there.然后我读到最好的做法是制作一个新的 venv 并在那里下载库。 I created a new environment:我创建了一个新环境: 在此处输入图片说明

Then I entered the new interpreter and added opencv-python (plus sign > searched for this > Install package):然后我进入了新的解释器并添加了opencv-python (加号>搜索了这个>安装包): 在此处输入图片说明

After that I made a short script that needs cv2 .之后,我制作了一个需要cv2的简短脚本。 Pycharm couldn't import cv2, but the script runs correctly (displays the image) when "run" is pressed. Pycharm 无法导入 cv2,但是当按下“运行”时脚本可以正确运行(显示图像)。

在此处输入图片说明

If I switch interpreter to the regular one it recognizes the cv2 methods and autocomplete me.如果我将解释器切换到常规解释器,它会识别 cv2 方法并自动完成我。

I solved it using anaconda .我使用anaconda解决了它。

The first time I tried it didn't work, so it's probably a problem with having non-ascii username ( again ).我第一次尝试它没有用,所以它可能是非 ascii 用户名的问题(再次)。 I had the .conda folder in a path which contained non-ascii, as well as the anaconda3 itself installed in a different place我的.conda文件夹位于包含非 ascii 的路径中,以及 anaconda3 本身安装在不同的位置

To install it correctly:要正确安装它:

  1. Uninstall both anaconda, pycharm (uninstall other pythons if you have them).卸载 anaconda 和 pycharm(如果有其他 python,请卸载它们)。

2. Create a new user with ascii only (eg Ben). 2. 创建一个只有 ascii 的新用户(例如 Ben)。 In this user:在这个用户中:

  1. Install anaconda (I guess miniconda will work too)安装 anaconda(我猜 miniconda 也可以)
  2. Create a venv (I used this and this tutorials)创建一个 venv(我使用了这个这个教程)
  3. Install your packages using conda-forge like in the tutorials像教程一样使用conda-forge安装你的包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM