python cannot find module(opencv) even though it is installed - python

I'm trying to install OpenCV on my raspberry pi 3(I'm using this tutorial https://linuxize.com/post/how-to-install-opencv-on-raspberry-pi/) after installation finished, I check it using the c++ library the module OpenCV has been installed
pi#raspberrypi:~ $ pkg-config --modversion opencv4
4.5.5
and when I use the python library the result is
pi#raspberrypi:~ $ python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
>>>
and then I check-in /usr/local/lib/python3.9/site-packages
pi#raspberrypi:/usr/local/lib/python3.9/site-packages $ ls
cv2

Related

PyTorch and OpenCV not working in same python

OpenCV and PyTorch can not be imported with the same python version.
I have installed Pytorch through anaconda and OpenCV from source build .so both can not be imported at the same time:
somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ python3
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
>>> exit()
somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ conda activate
(base) somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ python3
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
You need to make sure that you specify the correct options to cmake when building for your specific conda env.
Activate your conda env conda activate <name>
Install numpy with conda install numpy
Run cmake from the build directory using these options:
cmake \
-D CMAKE_INSTALL_PREFIX=<your choice> \
-D BUILD_opencv_python2=0 \
-D PYTHON3_LIBRARY=${CONDA_PREFIX}/lib/libpython3.9.so \
-D PYTHON_INCLUDE_DIR=${CONDA_PREFIX}/include/python3.9/ \
-D PYTHON3_EXECUTABLE=${CONDA_PREFIX}/bin/python \
-D PYTHON3_PACKAGES_PATH=${CONDA_PREFIX}/lib/python3.9/site-packages \
<path to opencv source>
CONDA_PREFIX contains the directory of the currently active environment., change python3.9 in case you have another python version.

How can I use globally installed CV2 (manual install) package into python virtual environment?

I have manually installed CV2 on Ubuntu box, and when I import CV2 it works perfectly.
If I create virtual environment (python -m venv myvenv), I am not able to use globally installed CV2.
Kindly advice the fix, to use global installed packages into python virtual environment.
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 as cv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
>>>

Anaconda does not find any module after Ubuntu upgrade to 18.04

After upgrading my Ubuntu OS from 16.04 LTS to 18.04.4 LTS, my python anaconda distribution does not find any regular module that should be in anaconda (numpy, matplotlib...), for instance, starting python from the command shell:
pjacquot#HP-EliteBook-Folio-1040-G3:python
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:51:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'matplotlib'
>>>
I can not even start spyder:
pjacquot#HP-EliteBook-Folio-1040-G3:~$ spyder
Traceback (most recent call last):
File "/home/pjacquot/anaconda3/bin/spyder", line 7, in <module>
from spyder.app.start import main
ModuleNotFoundError: No module named 'spyder'
I checked that I was using the "good" python:
pjacquot#HP-EliteBook-Folio-1040-G3:which python
/home/pjacquot/anaconda3/bin/python
and that the directory is in my path:
pjacquot#HP-EliteBook-Folio-1040-G3: echo $PATH
/home/pjacquot/anaconda3/bin:/texlive/2017/bin/x86_64-linux
Any idea what's happened there ?
I can still re install all modules, but it's going around the bush as they should be somewhere around here...
The not-clever solution: delete old anaconda (sudo rm -rf anaconda3) and reinstall it from scratch (takes 3 minutes and works...).

Importerror: No module named time in python2.7.6 in ubuntu

I have a strange problem on python2.7.6 with Ubuntu :
my python 2.7.6 is installed but strangely time module is not working. my ubuntu version is 16.04
i tried installing python 2.7.16 as well but that didnt fixed the issue as
/usr/local/bin# python2.7
Python 2.7.6 (default, Jul 22 2019, 12:49:04)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named time
expected : import time should work
actual : ImportError: No module named time
How do i import time in python 2.7.6
output of :
: ls /usr/lib/python2.7/lib-dynload/
audioop.i386-linux-gnu.so _codecs_tw.i386-linux-gnu.so dl.i386-linux-gnu.so _lsprof.i386-linux-gnu.so Python-2.7.egg-info
audioop.x86_64-linux-gnu.so _codecs_tw.x86_64-linux-gnu.so _elementtree.i386-linux-gnu.so _lsprof.x86_64-linux-gnu.so readline.i386-linux-gnu.so
_bsddb.i386-linux-gnu.so crypt.i386-linux-gnu.so _elementtree.x86_64-linux-gnu.so mmap.i386-linux-gnu.so readline.x86_64-linux-gnu.so
_bsddb.x86_64-linux-gnu.so crypt.x86_64-linux-gnu.so fpectl.i386-linux-gnu.so mmap.x86_64-linux-gnu.so resource.i386-linux-gnu.so
bz2.i386-linux-gnu.so _csv.i386-linux-gnu.so fpectl.x86_64-linux-gnu.so _multibytecodec.i386-linux-gnu.so resource.x86_64-linux-gnu.so
bz2.x86_64-linux-gnu.so _csv.x86_64-linux-gnu.so future_builtins.i386-linux-gnu.so _multibytecodec.x86_64-linux-gnu.so _sqlite3.i386-linux-gnu.so
_codecs_cn.i386-linux-gnu.so _ctypes.i386-linux-gnu.so future_builtins.x86_64-linux-gnu.so _multiprocessing.i386-linux-gnu.so _sqlite3.x86_64-linux-gnu.so
_codecs_cn.x86_64-linux-gnu.so _ctypes_test.i386-linux-gnu.so _hashlib.i386-linux-gnu.so _multiprocessing.x86_64-linux-gnu.so _ssl.i386-linux-gnu.so
_codecs_hk.i386-linux-gnu.so _ctypes_test.x86_64-linux-gnu.so _hashlib.x86_64-linux-gnu.so nis.i386-linux-gnu.so _ssl.x86_64-linux-gnu.so
_codecs_hk.x86_64-linux-gnu.so _ctypes.x86_64-linux-gnu.so _hotshot.i386-linux-gnu.so nis.x86_64-linux-gnu.so termios.i386-linux-gnu.so
_codecs_iso2022.i386-linux-gnu.so _curses.i386-linux-gnu.so _hotshot.x86_64-linux-gnu.so ossaudiodev.i386-linux-gnu.so termios.x86_64-linux-gnu.so
_codecs_iso2022.x86_64-linux-gnu.so _curses_panel.i386-linux-gnu.so imageop.i386-linux-gnu.so ossaudiodev.x86_64-linux-gnu.so _testcapi.i386-linux-gnu.so
_codecs_jp.i386-linux-gnu.so _curses_panel.x86_64-linux-gnu.so _json.i386-linux-gnu.so parser.i386-linux-gnu.so _testcapi.x86_64-linux-gnu.so
_codecs_jp.x86_64-linux-gnu.so _curses.x86_64-linux-gnu.so _json.x86_64-linux-gnu.so parser.x86_64-linux-gnu.so
_codecs_kr.i386-linux-gnu.so dbm.i386-linux-gnu.so linuxaudiodev.i386-linux-gnu.so pyexpat.i386-linux-gnu.so
_codecs_kr.x86_64-linux-gnu.so dbm.x86_64-linux-gnu.so linuxaudiodev.x86_64-linux-gnu.so pyexpat.x86_64-linux-gnu.so
It seems time package is not install, you can try by installing time package using below command.
pip install times
or
sudo pip install times.
and then try?

How to bind openalpr with python on a mac?

I recently installed openalpr on my mac using brew install openalpr with success. I would like to use the openalpr library with python 2.7 but I am having difficulty binding the two and could use some help.
I currently get the following in my projects file location:
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from openalpr import Alpr
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named openalpr
>>>
When I move to /usr/local/Cellar/openalpr/2.3.0/lib/python2.7/dist-packages/openalpr
The import works. How can I bind this library? Thanks
EDIT: I think I've seen that running the setup.py for openalpr is how you bind but I have no idea where to find it in my file system.
I had to do the following command:
echo /usr/local/opt/openalpr/lib/python2.7/dist-packages/ >> /usr/local/lib/python2.7/site-packages/openalpr.pth

Categories