2

I want to use Pytorch and OpenCV in Pycharm. So I created a virtual conda env and installed some packages to use.

When I execute Python in the Windows terminal and import torch and cv2, it worked well.

(Pytorch) C:\Users\choib>python
Python 3.7.1 | packaged by conda-forge | (default, Nov 13 2018, 19:01:41) [MSC v.1900 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import torch
>>>

It does not work normally with Pycharm. If I import cv2 before torch or torchvision or without them, it raises an error.

Example

import cv2 as cv
import torch

Result

D:\program\anaconda3\envs\pytorch\python.exe C:/Users/choib/Desktop/YOLO_01/YOLO_image.py
Traceback (most recent call last):
  File "C:/Users/choib/Desktop/YOLO_01/YOLO_image.py", line 1, in <module>
    import cv2 as cv
ImportError: DLL load failed: The specified module could not be found.

But if I import cv2 after torch or torchvision, it has no problem.

As a result, I don't have any problem to execute my code. But I wonder why I need to import torch or torchvision unnecessarily to avoid this ImportError.

Eskapp
  • 3,419
  • 2
  • 22
  • 39
HS. Choi
  • 121
  • 1
  • 8
  • Have you tried the solutions proposed in https://stackoverflow.com/questions/44668602/opencv-python-importerror-dll-load-failed-the-specified-module-could-not-be-fo and https://stackoverflow.com/questions/43184887/dll-load-failed-error-when-importing-cv2 ? You will probably need to uninstall opencv and re-install it. Also, in Pycharm, try to `Invalidate caches and restart`, this trick can solve some mysterious issues sometimes. – Eskapp Jan 31 '19 at 14:08
  • @Eskapp I'm very thank to your edits and comment. I solved the problem with links in your comment. Even though Pycharm code inspection warn that there is no module named `cv2`, it's not become serious problem to me. – HS. Choi Feb 01 '19 at 05:56

0 Answers0