penguin
penguin

Reputation: 846

Is it Possible to have Opencv installed for both Python 2 and Python 3 in Windows?

After installing Opencv for Python 3, I went on to try installing Opencv for python 2. I had a lot of trouble searching for the problem then after using dependency walker and downloading the missing dll, it worked. However IDLE for Python 3 gave me this message.

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import cv
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import cv
  File "C:\Python37\lib\site-packages\cv.py", line 1, in <module>
    from cv2.cv import *
  File "C:\Python37\lib\site-packages\cv2\__init__.py", line 3, in <module>
    from .cv2 import *
ImportError: DLL load failed: The specified module could not be found.
>>> import cv2
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import cv2
  File "C:\Python37\lib\site-packages\cv2\__init__.py", line 3, in <module>
    from .cv2 import *
ImportError: DLL load failed: The specified module could not be found.
>>> 

Edit: I think I may have to downgrade my Python 3 version. I am still open to suggestions though!

Upvotes: 0

Views: 1098

Answers (1)

Laurent LAPORTE
Laurent LAPORTE

Reputation: 22952

According to the PyPi repository, opencv is available for many versions of Python from 2.7 to 3.7. So, yes you can install it twice for Python 2.7 and 3.7 (if it is available for your OS).

But you need to have two Python environments, a.k.a. Virtualenvs! Of course Conda can solve this problem but you can also use the virtualenv tools. Follow the instructions to install it. It is a requirement for Python 2.

Choose a directory to store your virtualenvs, for Instance ~/virtualenv in your HOME. Create it if necessary.

For Python 2.7, you can run:

cd ~/virtualenv
virtualenv -p /path/to/python2 py2-demo
source py2-demo/bin/activate
pip install opencv-python

For Python > 3.3, you can use the venv module:

cd ~/virtualenv
python3 -m venv py3-demo
source py3-demo/bin/activate
pip install opencv-python

Upvotes: 1

Related Questions