Cannot import imaging from PIL - python

Traceback (most recent call last):
File "/home/raghav/Desktop/Everything/TinderAutomation-master/img_scrape.py", line 1, in <module>
from skimage.io import imread, imsave, imshow, show
File "/usr/local/lib/python3.7/dist-packages/skimage/io/__init__.py", line 7, in <module>
from .manage_plugins import *
File "/usr/local/lib/python3.7/dist-packages/skimage/io/manage_plugins.py", line 24, in <module>
from .collection import imread_collection_wrapper
File "/usr/local/lib/python3.7/dist-packages/skimage/io/collection.py", line 11, in <module>
from PIL import Image
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 60, in <module>
from . import _imaging as core
ImportError: cannot import name '_imaging' from 'PIL' (/usr/lib/python3/dist-packages/PIL/__init__.py)
I've problem importing imaging modules like matplotlib and skimage.io. I'm fairly new to python. Can anyone help me with this problem?

Try this: I first installed PIL (not Pillow) using
pip3 install --no-index -f https://dist.plone.org/thirdparty/ -U PIL
Then I found Pillow and installed it using
pip3 install Pillow
What fixed my issues was uninstalling both and reinstalling Pillow
pip3 uninstall PIL
pip3 uninstall Pillow
pip3 install Pillow

Related

Python loading wrong Pillow (PIL) package

I have strange issue while loading matplotlib package. Never observed that before. The package is trying to load PIL from the wrong place. If I try to load PIL from the command line, it also loaded from the wrong place.
Here is matplotlib traceback:
>>> import matplotlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/XXX/python_rep/miniconda3/envs/work/lib/python3.9/site-packages/matplotlib/__init__.py", line 113, in <module>
from . import _api, _version, cbook, _docstring, rcsetup
File "/home/XXX/python_rep/miniconda3/envs/work/lib/python3.9/site-packages/matplotlib/rcsetup.py", line 27, in <module>
from matplotlib.colors import Colormap, is_color_like
File "/home/XXX/python_rep/miniconda3/envs/work/lib/python3.9/site-packages/matplotlib/colors.py", line 51, in <module>
from PIL import Image
File "/homeappl/common-software/sw/python3.6/lib/python3.6/site-packages/PIL/Image.py", line 69, in <module>
from . import _imaging as core
ImportError: cannot import name '_imaging' from 'PIL' (/homeappl/common-software/sw/python3.6/lib/python3.6/site-packages/PIL/__init__.py)
Please note that I use my own conda environment with python 3.9
It is activated and contained Pillow version 9.0.0
Other packages are loaded from that instance, but bloody PIL tries to reach the common area.
/home/XXX/python_rep/miniconda3/envs/work/bin/python3.9 -m pip install pillow
Requirement already satisfied: pillow in /homeappl/common-software/sw/python3.6/lib/python3.6/site-packages (7.0.0)
I have no idea why it tries to load package from old place. But I do not have access to that - due to admin rights.
How to force machine not to use wrong package?

cannot import name '_registerMatType' from 'cv2.cv2' The other solutions don't work

I have been trying to run a script for object detection and I keep getting the following error,I have seen the same question being answered but I didn't get the desired result while reinstalling open cv
Traceback (most recent call last):
File "model_main_tf2.py", line 32, in <module>
from object_detection import model_lib_v2
File "/usr/local/lib/python3.7/dist-packages/object_detection/model_lib_v2.py", line 29, in <module>
from object_detection import eval_util
File "/usr/local/lib/python3.7/dist-packages/object_detection/eval_util.py", line 36, in <module>
from object_detection.metrics import lvis_evaluation
File "/usr/local/lib/python3.7/dist-packages/object_detection/metrics/lvis_evaluation.py", line 23, in <module>
from lvis import results as lvis_results
File "/usr/local/lib/python3.7/dist-packages/lvis/__init__.py", line 5, in <module>
from lvis.vis import LVISVis
File "/usr/local/lib/python3.7/dist-packages/lvis/vis.py", line 1, in <module>
import cv2
File "/usr/local/lib/python3.7/dist-packages/cv2/__init__.py", line 9, in <module>
from .cv2 import _registerMatType
ImportError: cannot import name '_registerMatType' from 'cv2.cv2'
The same thing occurred to me then i update my all opencv modules:
C:\Windows\system32>pip list |findstr opencv
opencv-contrib-python 4.5.3.56
opencv-python 4.5.2.52
opencv-python-headless 4.5.5.62
The version isn't same. So I first uninstall all opencv modules:
pip uninstall opencv-python-headless
pip uninstall opencv-python
pip uninstall opencv-contrib-python
Then reinstall all opencv modules in 4.5.5.62 version.
pip install opencv-python==4.5.5.62
pip install opencv-python-headless==4.5.5.62
pip install opencv-contrib-python==4.5.5.62
The problem is the resolved.

Python scripting inside PowerBI

I am reproducing a simple example from here to run Python into Power BI desktop :
https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-python-scripts
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'],dtype=float)
print (df)
I do not know where to begin to solve this error:
Details: "ADO.NET: Python script error.
<pi>Traceback (most recent call last):
File "PythonScriptWrapper.PY", line 2, in <module>
import os, pandas, matplotlib
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 174, in <module>
_check_versions()
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 159, in _check_versions
from . import ft2font
ImportError: DLL load failed while importing ft2font: The specified module could not be found.
</pi>"
This works :
pip uninstall matplotlib
pip install --upgrade matplotlib
pip uninstall pandas
pip install --upgrade pandas
pip uninstall pillow
pip install --upgrade pillow

Anaconda Python - how to reinstall NumPy

I am using Anaconda 5.3.0's Python interpreter in Visual Studio Code. When I try to import sklearn I get an error:
Traceback (most recent call last):
File "c:\Users\azzam\machinelearning.py", line 1, in <module>
import sklearn
File "C:\Anaconda3\lib\site-packages\sklearn\__init__.py", line 134, in <module>
from .base import clone
File "C:\Anaconda3\lib\site-packages\sklearn\base.py", line 10, in <module>
import numpy as np
File "C:\Anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Anaconda3\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Anaconda3\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Anaconda3\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: The specified module could not be found.
It looks like that I need to "reinstall" NumPy. I searched on the web, but I didn't find a way to "reinstall". There is only how to "install", and when I use
conda install numpy
in Anaconda Prompt I get:
Solving environment: done
# All requested packages already installed.
And if I tried to remove NumPy to install it again, it will remove everything, not just NumPy. So do I really need to "reinstall" NumPy to be able to import sklearn?
And if I do, how do I "reinstall" NumPy?
How to reinstall a package depends on the conda version.
newer versions (>= 4.6):
conda install numpy --force-reinstall
older versions (< 4.6):
conda install numpy --force
You will most likely have to uninstall NumPy and reinstall it.
conda remove numpy
And then install it again:
conda install -c anaconda numpy

ImportError: No module named 'numpy.ma'

The full error:
import matplotlib
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import matplotlib
File "R:\Python34\lib\site-packages\matplotlib\__init__.py", line 180, in <module>
from matplotlib.cbook import is_string_like
File "R:\Python34\lib\site-packages\matplotlib\cbook.py", line 34, in <module>
import numpy.ma as ma
ImportError: No module named 'numpy.ma'
numpy is imported normally.
How do I install numpy.ma?
I also faced the same situation today. I found that I had saved a file as numpy.py, so check the filenames in your folder.
Re-install the correct version of numpy.
download correct .whl file from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
pip install C:\Path\To\Wheel\Filename.whl # for example: numpy-1.9.2+mkl-cp34-none-win_amd64.whl
Use Your (via terminal) package manager and search.
Example on Ubuntu:
aptitude search numpy
and install package.
In my case:
apt-get install python-numpy

Categories