PyCharmでmatplotlibを使おうとしたところうまく行かなっかたので質問させてください
エラーになったコード
python
1import cv2 2import matplotlib 3import matplotlib.pyplot as plt 4 5img = cv2.imread("test.jpg") 6 7plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 8plt.show()
error
1Traceback (most recent call last): 2 File "/home/taise/PycharmProjects/practice_AI/imshow.py", line 3, in <module> 3 import matplotlib.pyplot as plt 4 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2349, in <module> 5 switch_backend(rcParams["backend"]) 6 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/__init__.py", line 833, in __getitem__ 7 plt.switch_backend(rcsetup._auto_backend_sentinel) 8 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 213, in switch_backend 9 switch_backend("agg") 10 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 221, in switch_backend 11 backend_mod = importlib.import_module(backend_name) 12 File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module 13 return _bootstrap._gcd_import(name[level:], package, level) 14 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 45, in <module> 15 from PIL import Image 16 File "/usr/lib/python3/dist-packages/PIL/Image.py", line 60, in <module> 17 from . import _imaging as core 18ImportError: cannot import name '_imaging' from 'PIL' (/usr/lib/python3/dist-packages/PIL/__init__.py) 19Error in sys.excepthook: 20Traceback (most recent call last): 21 File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook 22 from apport.fileutils import likely_packaged, get_recent_crashes 23 File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module> 24 from apport.report import Report 25 File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module> 26 import apport.fileutils 27 File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module> 28 from apport.packaging_impl import impl as packaging 29 File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module> 30 import apt 31 File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module> 32 import apt_pkg 33ModuleNotFoundError: No module named 'apt_pkg' 34 35Original exception was: 36Traceback (most recent call last): 37 File "/home/taise/PycharmProjects/practice_AI/imshow.py", line 3, in <module> 38 import matplotlib.pyplot as plt 39 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2349, in <module> 40 switch_backend(rcParams["backend"]) 41 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/__init__.py", line 833, in __getitem__ 42 plt.switch_backend(rcsetup._auto_backend_sentinel) 43 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 213, in switch_backend 44 switch_backend("agg") 45 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 221, in switch_backend 46 backend_mod = importlib.import_module(backend_name) 47 File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module 48 return _bootstrap._gcd_import(name[level:], package, level) 49 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 45, in <module> 50 from PIL import Image 51 File "/usr/lib/python3/dist-packages/PIL/Image.py", line 60, in <module> 52 from . import _imaging as core 53ImportError: cannot import name '_imaging' from 'PIL' (/usr/lib/python3/dist-packages/PIL/__init__.py) 54 55Process finished with exit code 1
試してみたこと
同じような内容の質問がありましたので参考にして
python
1import matplotlib 2matplotlib.use('TkAgg') 3import matplotlib.pyplot as plt
に書き換えてみましたがエラーになりました
error
1Traceback (most recent call last): 2 File "/home/taise/PycharmProjects/practice_AI/imshow.py", line 4, in <module> 3 import matplotlib.pyplot as plt 4 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2349, in <module> 5 switch_backend(rcParams["backend"]) 6 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 221, in switch_backend 7 backend_mod = importlib.import_module(backend_name) 8 File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module 9 return _bootstrap._gcd_import(name[level:], package, level) 10 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/backends/backend_tkagg.py", line 1, in <module> 11 from . import _backend_tk 12 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/backends/_backend_tk.py", line 6, in <module> 13 import tkinter as tk 14ModuleNotFoundError: No module named 'tkinter' 15Error in sys.excepthook: 16Traceback (most recent call last): 17 File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook 18 from apport.fileutils import likely_packaged, get_recent_crashes 19 File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module> 20 from apport.report import Report 21 File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module> 22 import apport.fileutils 23 File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module> 24 from apport.packaging_impl import impl as packaging 25 File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module> 26 import apt 27 File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module> 28 import apt_pkg 29ModuleNotFoundError: No module named 'apt_pkg' 30 31Original exception was: 32Traceback (most recent call last): 33 File "/home/taise/PycharmProjects/practice_AI/imshow.py", line 4, in <module> 34 import matplotlib.pyplot as plt 35 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2349, in <module> 36 switch_backend(rcParams["backend"]) 37 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 221, in switch_backend 38 backend_mod = importlib.import_module(backend_name) 39 File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module 40 return _bootstrap._gcd_import(name[level:], package, level) 41 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/backends/backend_tkagg.py", line 1, in <module> 42 from . import _backend_tk 43 File "/home/taise/.local/lib/python3.7/site-packages/matplotlib/backends/_backend_tk.py", line 6, in <module> 44 import tkinter as tk 45ModuleNotFoundError: No module named 'tkinter' 46 47Process finished with exit code 1
備考
~ $ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
~ $ python3 --version
Python 3.7.5
追記(半分解決しました)
ModuleNotFoundError: No module named 'apt_pkg'
は、
cd /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so
を実行することで解決しました.
ImportError: cannot import name '_imaging' from 'PIL' (/usr/lib/python3/dist-packages/PIL/init.py)
の方の解決策がわからないのでご教授お願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。