Search code examples
pythonpython-3.xopencvpycharmpython-venv

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 (e.g. 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:enter image description here

Then I entered the new interpreter and added opencv-python (plus sign > searched for this > Install package): enter image description here

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.

enter image description here

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


Solution

  • 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 (e.g. 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