简体   繁体   中英

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/

*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.

*I also tried to invalidate cache/ restart multiple times.

*I also tried to delete python and pycharm completely. When I upgraded to 3.10 I couldn't even install opencv. When I tried 3.8 again the same problem accoured.


I wanted to download ML libraries to 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).

Then I read it's better practice to make a new venv and download the libraries there. I created a new environment: 在此处输入图片说明

Then I entered the new interpreter and added opencv-python (plus sign > searched for this > Install package): 在此处输入图片说明

After that I made a short script that needs cv2 . Pycharm couldn't import cv2, but the script runs correctly (displays the image) when "run" is pressed.

在此处输入图片说明

If I switch interpreter to the regular one it recognizes the cv2 methods and autocomplete me.

I solved it using anaconda .

The first time I tried it didn't work, so it's probably a problem with having non-ascii username ( again ). I had the .conda folder in a path which contained non-ascii, as well as the anaconda3 itself installed in a different place

To install it correctly:

  1. Uninstall both anaconda, pycharm (uninstall other pythons if you have them).

2. Create a new user with ascii only (eg Ben). In this user:

  1. Install anaconda (I guess miniconda will work too)
  2. Create a venv (I used this and this tutorials)
  3. Install your packages using conda-forge like in the tutorials

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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