1

I'm trying to use opencv-python. I imported with pip but it throws ModuleNotFound.

Joshua-Tews-MacBook-Pro:~ nathan$ pip install opencv-python
Requirement already satisfied: opencv-python in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (4.0.0.21)
Requirement already satisfied: numpy>=1.14.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from opencv-python) (1.16.2)

I import with this:

import cv2

I get this error:

Traceback (most recent call last):
  File "/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/CEP/Y4/Capstone Project (Security Cameras System)/cep-ml-capstone/VideoProcessor.py", line 1, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

I've look at the answers on this stackoverflow thread, none seem to help: Cannot import cv2 in python in OSX

Others have solutions I completely didn't recognise pertaining to other OS so I skipped those.

Nathan Tew
  • 432
  • 1
  • 5
  • 21
  • Could you show the exact error and how you produced it? – Fenil Apr 02 '19 at 08:38
  • @GeneX okay i've updated my question. – Nathan Tew Apr 02 '19 at 08:42
  • May be an issue with the path you might want to check sys.path if it contains `/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-package` – Fenil Apr 02 '19 at 08:45
  • 1
    @GeneX oh dear, seems that I got 3.6 instead: printing sys.path gets me this: ['/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/CEP/Y4/Capstone Project (Security Cameras System)/cep-ml-capstone', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/Users/nathan/Library/Python/3.6/lib/python/site-packages', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages'] – Nathan Tew Apr 02 '19 at 08:47
  • Yup, you have two different python versions. Try setting path so that python command opens up python 3.7. Related question like this might help https://stackoverflow.com/questions/18425379/how-to-set-pythons-default-version-to-3-x-on-os-x – Fenil Apr 02 '19 at 08:55
  • @GeneX I'm not sure what changing the link does, the related question you mentioned was dealing with python2 vs python3... I tried the 2nd solution anyway and unlinked and linked to 3.7, but sys.path still gets me the same as above. And anyway, other modules work fine too, even though they are installed to 3.7 and sys.path gives 3.6. Any idea why? – Nathan Tew Apr 02 '19 at 09:02

1 Answers1

0

Okay I apparently I just had to run the script with Python 3.7 and it worked. I neglected to mention I was using Textmate.

To run scripts in Python 3.7 using Textmate, go to Preferences > Variables. If there isn't a variable named TM_PYTHON, create one then paste this in for the value: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3. Else, just change the value. Be sure to check the box next to the variable.

Nathan Tew
  • 432
  • 1
  • 5
  • 21