No module named cv2 error in Python Raspberry - python

I am having this error when I try to run a Python file. I tried opening a new terminal and importing cv2 worked there in Python but doesn't work in my OpenCV virtual environment. I don't even understand how virtualenvs work but I have to find a solution for this.
> (cv) pi#raspberrypi:~/Camerafeed $ python run.py Traceback (most
> recent call last): File "run.py", line 2, in <module>
> from camerafeed import CameraFeed File "/home/pi/Camerafeed/camerafeed/__init__.py", line 5, in <module>
> import cv2 ImportError: No module named 'cv2'

I referred below link to solve the error of "no module Named cv2":
https://qengineering.eu/install-opencv-4.4-on-raspberry-pi-4.html
I copied manually the cv2 as suggested by the above link as below:
For Python 2:
$ cd ~/opencv/build/lib/ $ sudo cp cv2.so /usr/local/lib/python2.7/dist-packages/cv2/python-2.7
For Python 3:
$ cd ~/opencv/build/lib/python3 $ sudo cp cv2.cpython-37m-arm-linux-gnueabihf.so \ /usr/local/lib/python3.7/dist-packages/cv2/python-3.7
Ensure that you create cv2 dir first if you got error.

python -c "import cv2" is a quick indicator to import module, you can use it in either of your environments as a check.
My guess is that you should install the module while you are in (cv) environment by running pip install opencv-python at command prompt.

I believe numpy is already installed. If not then sudo apt-get install python-numpy to install opencv2
Now you can run this command to install Opencv:
sudo apt update && sudo apt-get install python-opencv python-scipy ipython
For more help you can also refer to this link

Related

I tried to run a python script, but got ImportError: attempted relative import with no known parent package

I found other stackoverflow questions and answers for this error, but none of them worked for me. My python package problem:
I tried to install packages with these commands so I could run tests:
$ pip3 install virtualenv
$ virtualenv -p $(which python3.9) venv
$ source venv/bin/activate
$ pip install --upgrade pip # stop local environments from messing things up
$ pip install -r path/to/requirements.txt
I tried to run my tests:
$ python -m unittest discover
I got this error:
======================================================================
ERROR: some_file (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: some_file
Traceback (most recent call last):
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/Users/path/some_file.py", line 3, in <module>
from .other_file import some_function, SomeClass
ImportError: attempted relative import with no known parent package
----------------------------------------------------------------------
As the error said, I was trying to use a relative path in my file:
from .other_file import some_function, SomeClass
I don't know about other versions of python, but I expected this to be possible in python3.
I needed to add this line to my installation commands:
$ pip install --editable .
That is:
$ pip3 install virtualenv
$ virtualenv -p $(which python3.9) venv
$ source venv/bin/activate
$ pip install --upgrade pip # stop local environments from messing things up
$ pip install -r path/to/requirements.txt
$ pip install --editable .
I don't yet understand why it works. I'll update this answer if I find out more.

python install pip with error "ModuleNotFoundError: No module named '_struct'"

I install python from source:
$ wget -c https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
$ tar xf Python-3.6.1.tar.xz
$ cd Python-3.6.1
$ ./configure --enable-optimizations --prefix=$HOME/.local/ --with-pydebug
$ make altinstall -j8
Then I install pip with get-pip.py
$ python3.6 get-pip.py --prefix=~/.local/
I met the error of:
Traceback (most recent call last): File "get-pip.py", line 27, in
import struct File "/home/zhangzy/.local/lib/python3.6/struct.py", line 13, in
from _struct import * ModuleNotFoundError: No module named '_struct'
What is the cause of this problem and how could it solve it?
You should always use your system package manager when possible. There is a safe way to get newer versions of Python on CentOS/RHEL, as proper RPM packages. It is called the IUS project, and we offer multiple versions of Python.
After setting up the repo, you can get Python 3.5 by running the command yum install python35u. You will then have the command python3.5 available, as well as the pyvenv-3.5 command for creating virtual environments.

s3cmd ImportError: No module named S3.Exceptions

Received an error after installing and trying to run s3cmd 1.0.0
s3cmd -h
Problem: ImportError: No module named S3.Exceptions
S3cmd: unknown version. Module import problem?
Traceback (most recent call last):
File "/usr/bin/s3cmd", line 1995, in <module>
from S3.Exceptions import *
ImportError: No module named S3.Exceptions
Your sys.path contains these entries:
This error came about after upgrading to the latest Amazon Linux distro 2015.03.0
Looks like the error happened because python2.7 is now the default python version in the Amazon Linux 2015.03.0+ If you change python back to 2.6 and run s3cmd it should work without a problem
update-alternatives --set python /usr/bin/python2.6
s3cmd -h
After the s3cmd command is ran you can put python back to 2.7 for yum and other utilities:
update-alternatives --set python /usr/bin/python2.7
yum install <package>
vi /usr/bin/s3cmd
add 2.6 to the first line, so it looks like:
#!/usr/bin/python2.6
Save thefile and s3cmd will work. as long as you have /usr/bin/python2.6 on your system
I faced a similar error with s3cmd, but module name was different:
ImportError: No module named S3.ExitCodes
In my case I could solve the problem this way:
yum install python-pip and then pip install s3cmd.
After that s3cmd worked fine.
Neither of the previous answers worked for me, but copying a few lines from the sourcegraph aws-cli dockerfile did:
FROM python:2
RUN apt-get update -q
RUN apt-get install -qy python-pip groff-base
RUN pip install awscli
s3cmd was working with one version of my python in sys.path, it was keeping up some other version.
sudo vi /usr/bin/s3cmd
You will see this in starting lines.
import sys
I had two python version python3.5 and python3.6
Lets run python3.5 and then python3.6
import sys
print('\n'.join(sys.path))
Match your sys path with s3cmd error sys path. This will give you assurance that you need to change the python version in sys path for that.
Now: How to Change sys.path python version?
echo $PYTHONPATH
sudo vi ~/.bashrc
export PYTHONPATH=/usr/bin/python3.6
PYTHONPATH Help Link

RPi SPI spidev python

I am trying to get data from sensors with mcp 3002.
I always get this error:
importError: no module named spidev.
I have tried the following:
mkdir py-spidev
cd py-spidev
wget https://raw.github.com/doceme/py-spidev/master/setup.py
wget https://raw.github.com/doceme/py-spidev/master/spidev_module.c
sudo python setup.py install
but got the error
spydev_module.c:no such file or directory
What can I do?
I think you have to define the path correctly.
cd \examplepath\examplefile\
Then install the module by Python setup.py install
Try installing with python3 since it appears you have multiple python versions on your computer:
sudo python3 setup.py install

mysqlfailover: No module named mysql.utilities.common.tools

I am trying to setup mysqlfailover utility on ec2 instance running Ubuntu 12.04. I performed the following steps:
Downloaded mysql-connector-python:
wget http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python_1.0.12-1debian6.0_all.deb -o mysql-connector-python_1.0.12-1debian6.0_all.deb
(Installed using dpkg -i command.)
Download mysql-utilities:
wget http://cdn.mysql.com/Downloads/MySQLGUITools/mysql-utilities_1.3.5-1debian6.0_all.deb -o mysql-utilities_1.3.5-1debian6.0_all.deb
(Installed using dpkg -i command.)
There were some dependencies issues. I ran the following commands:
sudo apt-get install python-pip
easy_install -U distribute
sudo apt-get install libmysqlclient-dev
sudo apt-get install python-dev
pip install mysql-python
Now, when I run the command mysqlfailover, it gives the following error:
Traceback (most recent call last):
File "/usr/bin/mysqlfailover", line 24, in <module>
from mysql.utilities.common.tools import check_python_version
ImportError: No module named mysql.utilities.common.tools
I have been banging my head around quite some time now, but no success. Any help in the matter is highly appreciated. Thanks.
Try env PYTHONPATH=/usr/share/pyshared/ mysqlfailover
Thers is no utilities in mysql modul, you can check this do:
ls /usr/lib/python2.7/dist-packages/mysql
So you should change mysql package:
cp -r /usr/share/pysharedmysql /usr/lib/python2.7/dist-packages/mysql

Categories

Resources