1

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.

benjamin
  • 304
  • 4
  • 13
  • could please add the terminal screenshot of PyCharm? – Sabil Oct 09 '21 at 12:51
  • @Sabil I can, but it doesn't print an error message. After running the terminal is empty, and after I press a button it exists with code of 0. – benjamin Oct 09 '21 at 12:54
  • does any of this help? https://stackoverflow.com/search?q=%5Bpycharm%5D+cv2+cannot+find+reference – Christoph Rackwitz Oct 09 '21 at 13:50
  • The only requirement for the library to be recognized is correctly setting the default interpreter for the project in settings. You've only already done that so it should work. Since the code works at run-time it's obviously the IDE file that indexes the venv's that needs refreshing - Try to clear the cache! If you run this in the terminal remember the venv needs to be activated, if you're using run configurations you'll also have to choose the right venv. But the warning in the editor window is only connected to the interpreter chosen in the settings. – bad_coder Oct 09 '21 at 14:55
  • @bad_coder I invalidated the cache and restart multiple times. I run via the run button on top right, and in my run configurations I chose the right venv. – benjamin Oct 09 '21 at 15:09
  • @bad_coder unfortunately it doesn't, I'm trying the other threads Christoph suggested now – benjamin Oct 09 '21 at 15:10
  • @benjamin if you did invalidate the cache the other possible explanation is that the stub files are missing from the package as you installed it. I think [this answer](https://stackoverflow.com/a/61192749) might work for you. The way [opencv is organized](https://pypi.org/project/opencv-python/) can be rather complicated. It seems you need to manually set the windows DLL on the path after installation. See [this list](https://stackoverflow.com/q/8822335) for some extra context about the other answer. – bad_coder Oct 09 '21 at 15:16
  • may I recommend adding those thoughts also to the other question that this question is marked a duplicate of? – Christoph Rackwitz Oct 09 '21 at 17:15
  • @bad_coder I tried it now, but it didn't work (I didn't have a DLLs folder so I made one, tried both with and without deleting the file in `site-packages/cv2/`) – benjamin Oct 09 '21 at 17:37
  • @benjamin I think you solved it. When you say at the bottom of the thread: *"If I switch interpreter to the regular one it recognizes the cv2 methods and autocomplete me."* that's it. – bad_coder Oct 09 '21 at 17:42
  • @bad_coder I thought you need venvs just so you don't end up downloading all your packages to the same interpreter. That's true? If so, then I wanted a new interpreter for a new venv, and in there I downloaded opencv for example. I mean- I know I have a working interpreter right now, but it supposedly bad practice to use it. – benjamin Oct 09 '21 at 17:45
  • @benjamin yes you are supposed to use venvs. If you want edit the question to include the steps you have taken that didn't work for you, with links to the other answers you've tried. I get the impression you are using the venv. (Did you actually install packages -opencv- to the Python base installation? Because if you did that's wrong and might be the source of the error. Start from a clean Python base installation, create the venv like you showed, install opencv and it should work). – bad_coder Oct 09 '21 at 17:51
  • @bad_coder I deleted `opencv-python` from my base interpreter and made a new venv with it. Then I downloaded `opencv-python` to the venv interpreter. Didn't work (also added it to the thread) – benjamin Oct 09 '21 at 18:26
  • @benjamin I would advise starting from a clean Python base installation. Removing packages should be done [also using pip](https://docs.python.org/3/tutorial/venv.html#managing-packages-with-pip). If you simply delete you can miss some files and subsequent errors can be hard to diagnose. (Always start with a clean base installation and don't change it). Anyway, I'm casting a reopen vote. (Also unselect the "make available to all projects" and "inherit site packages" in the 1st screenshot). – bad_coder Oct 09 '21 at 18:38
  • @bad_coder What do you mean by "starting from a clean Python base installation"? Deleting python itself and downloading again? – benjamin Oct 09 '21 at 19:58
  • @benjamin you don't have to delete the current Python, just make a new installation in a different directory (that's how you can have several Python's on the system at the same time). You can try it without having to bother with setting the environment variables. – bad_coder Oct 09 '21 at 20:13

1 Answers1

0

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
benjamin
  • 304
  • 4
  • 13