Problems importing open cv in python - python-3.x

So I've been working with the Movidius neural network api, and it automatically installs several packages on the raspberry pi when you run the installer, including opencv. However, when I try to
import cv2
I get the error
from .cv2 import *
ImportError: libQtGui.so.4: cannot open shared object file: No such file or directory
does anyone know how to fix this?

Assuming you are using Ubuntu mate or Raspbian stretch OS with python3.
If you are still facing issue with importing cv2,
Try to install the following.
pip3 install opencv-python
sudo apt install libqtgui4
sudo apt install libqt4-test
This will pull all the dependencies and you can import cv2 without any issues.

You might have done some system changes which led to the path mismatch of libQtGui.
there are steps to solve this problem but it will take more time than installing OpenCV again with the configured libQtGui path. So I suggest you install your open cv version again.
below is the link to install OpenCV from scratch it hardly takes 20 minutes.
https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv/

Related

pip list cmd shows tensorflow installed but cant import it

I have created a virtual environment with python 3.6 and installed various packages using pip, when i do pip install tensorflow it says "Requirement already satisfied". However when i import it, I get No module name tensorflow.python
And as every guy should know the very first solution of every problem...
Uninstall and reinstall
Yep, It worked.

Cannot Import CV2 on Spyder (Python 3.7)

I've tried downloading opencv through pip, conda and downloading whl file from https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame , no luck so far.
I tried
pip install opencv-contrib-python ,
pip3 install opencv-contrib-python ,
pip install opencv-python,
pip3 install opencv-python
All give same result, after successful installation, I get cv2 installed without an actual cv2.py file inside it, there is a folder CV2 inside site packages, which has looks like bellow:
and receive no module named cv2when asking to import cv2 on Spyder. When I use the ame command in the Python Shell, it imports with no issues and runs cv2.imread also with no issue, so am not quite sure if its a Python or a Spyder issue.
I also used anaconda 3 conda install, there exist issues between h5py package and opencv package, have not been able to download. I use windows 10. Ive tried the method outlined here:
Cannot find module cv2 when using OpenCV
Ive installed, uninstalled and reinstalled python 3.7, Can anyone direct me on any other paths to take?
So I did the following to be able to run import cv2 from Spyder 3 (built in with Anaconda): Uninstall and Reinstall Anaconda 3 and adding it to the PATH. I then copied everything inside the cv2 folder in the python folder (shown in screenshot in original question) and added it to the Scripts Folder in the Anaconda Directory (where it has been downloaded).
My understanding is the issue arised from Spyder not sourcing the Python directory although the interpreter is set as python and the wd set as a subfolder of the python directory (whithin which the cv2 folder exists). Downloading directly from Conda didnt work due to compatibility issues between h5py and opencv. Check if you can import cv2 from Python shell directly, if so its a IDE issue - took me two days to figure it out.

tkinter is not recognized as a library when importing it on a pycharm project using python 3.x ¿Why?

if I try to import tkinter in my project, pycharm underlines tkinter as being a missing library.
When I try to install the library, pycharm suggests to import the 'future' library instead of the tkinter library. I don't know why.
If I go to the project interpreter window, I cannot find the tkinter library after clicking on the install packages button.
On the pycharm terminal, if I try to pip install tkinter it returns:
Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter
I get the same result when trying pip install python-tk, pip install python3-tk, and pip install tk.
Does anyone know why this is?
I imported the project from windows into Ubuntu Mint 19.1, I am using interpreter Python 3.7.2.
Please let me know if I can add any more useful info.
Install it via apt.
sudo apt-get update && sudo apt install python3-tk
Works for me.
I fixed the issue by uninstalling PyCharm, and reinstalling it from the command line. The previous version I had installed it through the Linux Mint 19.1 Software Manager, and the version installed behaved in very strange ways; for instance it showed a completely different file tree to the one in my machine when trying to setup interpreters.
Here is the command I used, as per JetBrains recommendation:
sudo snap install [pycharm-professional|pycharm-community] --classic
Note: I had to install snap first :)
tkinter is a built in library in python.
install this package :'future' using this command :
pip install future
now import 'tkinter' in your project :
import tkinter

ImportError:'save_weights' requires h5py

When I save weights during training my CNN model using keras, it says ImportError:'save_weights' requires h5py, but I have already installed h5py.
I would greatly appreciate if someone could explain how to fix this issue.
Just install necessary packages
sudo apt-get install libhdf5-dev
pip install h5py
If you are using windows and python IDE, open cmd and input following commands:
pip install h5py
pip install cython
I hope it helps.
I was getting the same error as you.
I installed all the requirements listed here: https://github.com/fchollet/keras/issues/3426
Finally just needed to reboot and it started working.
As suggested by others:
pip install h5py
Note that this may not immediately resolve the issue in your active session and you may need to reload keras.models either through the following commands or by just creating a new session/re-opening your jupyter notebook.
In Python3:
from importlib import reload
reload(keras.models)
In Python2:
use importlib.import_module instead. See docs for a reference.
These additional steps may be necessary because of the try/except ImportError in keras sourcecode that assigns h5py = None when it's unable to locate it the first time it's executed.
In my case, re-installing did the trick:
pip uninstall -y cython h5py
pip install cython h5py
(Windows 10, Conda, Keras 2.4.3)
I think you may miss this
from keras.applications import imagenet_utils
I got the same problem even though I have imported the h5py.
It is the load error with the keras. It has to be reloaded.
import keras
from importlib import reload
reload(keras.models)
It has worked for me.
h5py==2.10.0 works well with TF >= 2.1 so try 'pip install h5py==2.10.0'
Have you tried directly installing h5py? http://docs.h5py.org/en/latest/build.html
Try running:
pip install h5py
or
sudo apt-get install libhdf5

Get mayavi working with Python 3

I'm having trouble running mayavi in python 3.3
When running my program with python3 (or python3.3) i get
from mayavi import mlab as ml
ImportError: No module named 'mayavi'
To get numpy and matplotlib to work, i used:
sudo apt-get install python3-"missing module"
But the usual way to install mayavi is just
sudo apt-get install mayavi2
And python3 doesn't find it....
Does anyone know a way to get mayavi to work with Python3?
Is it not supported yet?
I have tried both
import mayavi.mlab as ml
from mayavi import mlab as ml
Both give me the same error
Any clarification would be greatly appreciated :)
Mayavi can work with python 3 now. You will need VTK 7.0 or higher compiled for python 3 support. Then you can just install mayavi with pip. pip install mayavi
Installing vtk may prove somewhat difficult. You need to make sure to change the PYTHONPATH variable, and build vtk with tcl, tk, and python 3 support.
Today, Mayavi is not supported in python3.
http://www.vtk.org/Wiki/VTK/Python_Wrapping_FAQ
http://www.vtk.org/Wiki/VTK/Python_Wrapper_Enhancement#Python_3
See also this github issue:
https://github.com/enthought/mayavi/issues/84
I was able to get Mayavi2 to work with Python 3.6.4.
To set up Mayavi on ubuntu, do the following -
Install Anaconda/Miniconda distribution of python depending on your
needs(I used Miniconda 4.3.30). For more info look here
Run the following in terminal -
apt-get install -y libglib2.0-0 libxext6 libsm6 libxrender1 gtk3.0
conda install -y -c conda-forge qt vtk pyvtk pyside envisage mesalib mayavi
conda install -y -c ajsrk traitsui
Ideally , you wouldn't need the last line since traitsui will be installed as part of the dependencies in the previous step. However, I found an issue with this version of traitsui. So as a temporary fix, I have uploaded my version of traitsui into anaconda cloud; this can be used until the fix finds its way into the next release and is available in the conda-forge channel
To verify the installation start a terminal session and type 'mayavi2'
The best solution that I`ve found, is this. Even though, Mayavi will only work on Pyathon 3.5...
My solution is launching Spider from Anaconda 3 and then running the file mayavi2-script.py that can be found in the directory Anaconda/Scripts. It launches the 3D data visualizer as a pop-up window in Windows10.

Resources