import cv2 terminal error - python - python

I'm trying to simply install and use cv2, numpy and matplotlib.
All installed successfully but when I type into the terminal "import ..." it gives me the following error?
I'm confused because what the terminal is suggesting, I've already installed it that way?
I'm not sure about why it's pointing me to the packages either?
import cv2
The program 'import' can be found in the following packages:
* imagemagick
* graphicsmagick-imagemagick-compat
Try: sudo apt-get install <selected package>
Thanks

import is a command from imagemagick and graphicsmagick, you are trying to import a module python in you terminal, but, you should import in python shell:
$ python
Python 3.6.3 (default, Oct 9 2017, 12:11:29)
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> code_here
>>> exit()
OVERVIEW from import from imagemagick.
The `import` program is a member of the ImageMagick(1) suite of tools. Use it to capture some or all of an X server screen and save the image to a
file.
For more information about the import command, point your browser to file:///usr/share/doc/ImageMagick-6/www/import.html or http://www.imagemagā€
ick.org/script/import.php.

Related

Problems with OpenCV install on Raspberry Pi

I have a Raspberry Pi running Raspian 10. I installed OpenCV using this tutorial. The steps seemed to all complete successfully, but I've had some problems importing the python module. When I use Python 3.7.3, I get the following output:
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
Which is clearly not working. Using Python 2.7.16 however, it runs fine:
>>> import cv2
>>> cv2.__version__
'4.6.0-dev'
Any idea why this may be? It seems like the OpenCV Python module isn't installed globally for Python 3. Ideally, I'd like to be able to use it in a Python 3 virtual environment.
I do have a temporary workaround. If I cd into the build directory containing the module, I can get it to work:
pi#raspberrypi:~ $ cd opencv/build/python_loader/
pi#raspberrypi:~/opencv/build/python_loader $ ls
cv2 setup.py
pi#raspberrypi:~/opencv/build/python_loader $ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.6.0-dev'
Obviously, this isn't ideal.
I've managed to answer my own question. Running:
pi#raspberrypi:~/opencv/build $ pip3 install -e python_loader
Installs the package from the folder containing the module. It's not the most elegant solution, and I'd still be interested to see if anyone knows why the original method didn't work.
Hope this can be of some help to someone.

altair python package gives "Illegal instruction" on Raspberry PI

In my RaspberryPI ZERO W I got strange error when trying to import altair module in python script.
pi#raspberrypi:~ $ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import altair
Illegal instruction
pi#raspberrypi:~ $ pip3 freeze
altair==4.2.0
Does anybody have an idea if there is a version, which works on RaspberryPi? Or any other idea why it is happening?
Illegal Instruction means that you are importing a C extension that is compiled incorrectly for the system you are using. Altair itself doesn't have any C extensions, but it depends on a number of libraries that do include C extensions, which you can see in its requirements.txt file:
entrypoints
jinja2
jsonschema>=3.0
numpy
pandas>=0.18
toolz
I would suggest trying to import each of these libraries to narrow-down which package is incorrectly installed, and then search for information on how to correctly install the problematic package on RaspberryPi ZERO.

OpenCV 3.4.1 on Mac through Anaconda - - package not found

I'm running an Anaconda virenv that has successfully installed OpenCV ver.3.4.1. From the python prompt I can import cv2 and can confirm it's version per the command line below:
Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.1'
However, in VSCode I run the following simple program and get an error:
import numpy as numpy
import argparse
import cv2
pass
The error is:
Traceback (most recent call last):
File "detect_faces.py", line 3, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
I don't understand why the command line import works, but the program from VSCode fails to import the same package.
There are multiple SO Q&A on this topic but they are all incredibly time/date sensitive and platform specific -- most deal with the situation where the package itself isn't recognized anywhere. The closest question is this one which is from 2015 and deals with the Android platform. Any thoughts?
Update
The version installed should be 3.5.5, but I noticed that in the command prompt launched from anaconda it's shown as Python 3.5.5 :: Anaconda, Inc. while the version shown within VSCode is Python 3.6.5 :: Anaconda, Inc.. When I look at the available env in VSCode it shows some 2.7s and several 3.6 versions - including the one I'm using which is 3.6.5.
I've just done a new install of a fresh Python 3.6 env and noticed that the command prompt reports Python 3.6.6 while the output from a sys check reports Python 3.6.5 -- again, both have the same executable path.
Although I was unable to replicate this same error, I did determine that it had to do with the installation of numpy. For whatever reason, it originally installed and downgraded the Python version within the venv. Then later when upgrading python it created a mismatch. The fix was to recreate the venv with a pip install of all three packages in the same command line. When installing all three at once the packages all reconcile - - I don't know if there was another variable - I couldn't recreate the original error.

ipython using python3.4 rather than python3.6

I'm working on a ubuntu 14.04 machine with python3.6 installed. When I bring up the python shell using the "python3" command it invokes the python3.6.1 version as shown below and I'm successfully able to import pandas library
python3
Python 3.6.1 (default, Jun 13 2017, 21:37:44)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>>
But when I invoke the ipython shell by the command ipython3, it is using python 3.4.3 as shown below and I'm unable to import the pandas library. How do I tell ipython3 to use python3.6.1 version?
ipython3
Python 3.4.3 (default, Nov 17 2016, 01:11:57)
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import pandas
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-d6ac987968b6> in <module>()
----> 1 import pandas
ImportError: No module named 'pandas'
How can I deal with multiple versions of python to avoid such problems?
I've had this problem when I've accidentally been in the wrong environment invoking ipython (or a new environment where it isn't installed yet). If you have ipython installed in your root environment or anywhere else on your path it will default to that instead, fairly silently.
To check do a 'pip list', where you are invoking ipython. If it isn't listed, install it with 'pip install ipython' and then rerun.

ImportError: No module named tensorflow, but tensorflow does exist

I installed tensorflow with virtualenv on linux. There is a tensorflow package under sitepackage folder, but when I run the demo downloaded from Github, it shows:
(tensorflow) idc#idc-Hi-Fi-Z77X:~/tensorflow$ sudo python CNN_sentence_tensorflow-master/sentence_classfier_with_tensorflow.py
Traceback (most recent call last):
File "CNN_sentence_tensorflowmaster/sentence_classfier_with_tensorflow.py", line 13, in <module>
import tensorflow as tf
ImportError: No module named tensorflow
I do this at interface. It's ok:
(tensorflow) idc#idc-Hi-Fi-Z77X:~/tensorflow/multi-class-text-classification-cnn-master$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import tensorflow as tf
>>>
you are using sudo python CNN_sentence_tensorflow-master/sentence_classfier_with_tensorflow.py
if you use sudo i think it will use your main python version not the one in your virtualenv
Because sudo python basically means run python as some other user (root by default). That user may have a different set of environment variables, including $PATH.
Some of linux distributions use older Python version for root user,like centos.Have a look at the outputs of which python and sudo which python, you'll see they might be different.
[~]$ which python
/usr/local/bin/python
[~]$ sudo which python
/usr/bin/python
Maybe you don't need to use sudo,or you can set permissions to all files and folders by using chmod -R 755 /folder.
Hope this helps.

Categories

Resources