cannot import module "cv2" in python on mac - python

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.

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.

Related

Says I don't have numpy, then it says it is already installed

I am having issues with the numpy library. I am trying to code a webcam using the opencv library, but I am getting this issue:
OpenCV bindings requires "numpy" package.
Install it via command:
pip install numpy
Traceback (most recent call last):
File "/home/pi/Documents/security-camera/take_picture.py", line 1, in <module>
import cv2
File "/home/pi/.local/lib/python3.9/site-packages/cv2/__init__.py", line 12, in <module>
import numpy.core.multiarray
ModuleNotFoundError: No module named 'numpy.core.multiarray'
When I try to install numpy using pip, I get this error:
pi#raspberrypi:~/Documents/security-camera $ python3 -m pip install numpy
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (1.19.5)
It says I already have the numpy library, even though when I run it says to install it. Does anyone know how to fix it?
I figured out how to solve this issue: I was getting an issue with numpy earlier and was trying to reinstall it, so I deleted the numpy folder in /usr/lib/python3/dist-packages, then I tried to install it again but it did not work. Then I got this issue. There are actually two numpy folders in /usr/lib/python3/dist-packages, so when I deleted the one with the python files in it, and imported numpy in the python program, it says numpy did not exist, but when I tried to reinstall numpy with pip, it said it already existed because it looked at the other folder. So what I had to do was delete the other folder in /usr/lib/python3/dist-packages, and then reinstalling numpy worked.

python: installing pygame to every new project

I ran into some problems with some pip installations. I have found a way of installing that works, but I feel like it's wrong.
I installed pygame from the cmd prompt and got this message:
Defaulting to user installation because normal site-packages is not
writeable Requirement already satisfied: pygame in
c:\users\Somenewguy\appdata\roaming\python\python310\site-packages
(2.1.2)
But when I went to pycharm and started my project, I got this message:
Traceback (most recent call last):
File "C:\Users\Somenewguy\Python Projects\new\main.py", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
I then did the pygame pip installation from the terminal in pycharm. But this time I installed it directly into the project folder, and that approach worked.
This is the only way I can import pygame into my projects which I think is weird.
Why do I have to install this package for each project?
If you use a virtual-environment, this is the expected behavior. If you don't want to install it every time, use the system environnement.

Trying to install libraries in pypy

I knew that pypy is faster than CPython so I wanted to try it and it is actually way faster than CPython, however when I install libraries it shows the following:
PS C:\Users\DELL\Desktop\telegram\python -work some-> pip install pillow
Requirement already satisfied: pillow in c:\users\dell\appdata\roaming\python\python39\site-
packages (8.3.0)
but when I try to import the module (pillow):
Traceback (most recent call last):
File "c:/Users/DELL/Desktop/telegram/python -work some-/studying code 2.py", line 1, in
<module>
import pillow
ModuleNotFoundError: No module named 'pillow'
it is not there! please help!
pillow is an up-to-date fork of the now deprecated Python Image Library (PIL). you need to import PIL or from PIL import ... to use it.
Check out their documentation to learn more!
You must run pip with the version of Python you want to install the package for. In this case, you need pypy3 -m pip install pillow or something similar---run it with the same interpreter as the script you'll run next. In your pasted output, we see Requirement already satisfied: pillow in c:\users\dell\appdata\roaming\python\python39\site-packages which is the path for CPython 3.9, not PyPy.

ModuleNotFoundError - Cannot install OpenCV for Python 3 on Mac

I am having some trouble downloading and importing the OpenCV library. In my Terminal, I executed
pip3 install opencv-python
and I got the following response:
Requirement already satisfied: opencv-python in ./opt/anaconda3/lib/python3.8/site-packages (4.4.0.42)
Requirement already satisfied: numpy>=1.17.3 in ./opt/anaconda3/lib/python3.8/site-packages (from opencv-python) (1.18.5)
I then tried to start a python3 session in my Terminal, and I could execute import cv2 successfully without any errors. However, when I tried to execute import cv2 in Visual Studio Code, I get the following error:
Traceback (most recent call last):
File "motiondetector.py", line 1, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
I also get the same error when I am running my code on IDLE.
Does anyone know why this errror occurs? Any help would be much appreciated. Thanks in advance!
Just found the answer - I had to install the OpenCV module in the IDE as well! Thank you to everyone who helped! :)

I install bs4, but then get "No module named 'bs4'"

UPDATE: Thank you to everyone who answered and commented. I understand now that I have 2 versions of python installed. My program was running from "Miniconda3/python.exe ". While bs4 was installed in "c:\python38\lib\site-packages". I understand this to be the cause of the problem. I removed Minicoda3 but VS code still tries to use it when I run a program. How can I fix this?
When I run the following code:
>>> from bs4 import BeautifulSoup
Error:
I get the error: "No module named 'bs4'.
After I install bs4, It says "Requirement already satisfied" yet I get the same error.
Im not sure what I'm doing wrong, please help. Error messages below.
PS C:\Users\Admin\Desktop\exAPP> pip install bs4
Collecting bs4
Requirement already satisfied: beautifulsoup4 in c:\python38\lib\site-packages (from bs4) (4.9.1)
Requirement already satisfied: soupsieve>1.2 in c:\python38\lib\site-packages (from beautifulsoup4->bs4) (2.0.1)
Using legacy setup.py install for bs4, since package 'wheel' is not installed.
Installing collected packages: bs4
Successfully installed bs4-0.0.1
PS C:\Users\Admin\Desktop\exAPP> & C:/Users/Admin/Miniconda3/python.exe c:/Users/Admin/Desktop/JNB.py
Traceback (most recent call last):
File "c:/Users/Admin/Desktop/JNB.py", line 2, in <module>
import BeautifulSoup
ModuleNotFoundError: No module named 'BeautifulSoup'
PS C:\Users\Admin\Desktop\exAPP> & C:/Users/Admin/Miniconda3/python.exe c:/Users/Admin/Desktop/JNB.py
Traceback (most recent call last):
File "c:/Users/Admin/Desktop/JNB.py", line 2, in <module>
from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'
PS C:\Users\Admin\Desktop\exAPP>
You are using miniconda, which might not be on your PATH correctly such that pip is actually part of a separate Python installation.
You could use conda install pip, or C:/Users/Admin/Miniconda3/python.exe -m pip install bs4
Okay so my problems are fixed an my program is running. Like I said in the update the first problem was due to having 2 versions of python installed. After I removed the unwanted version it was still set as the python interpreter path. I was trying to fix this by going into VS code settings but was having no luck. Fortunately I finally noticed there was a button on bottom left of the screen that allowed me to change the path easily. Thanks again to those who commented and answered.

Categories

Resources