[python]ubuntuにmatplotlibを入れようとしてハマる

ubuntuでmatplotlibを使いたい

正面切ってインストールしてみる

$ sudo pip install scipy

下記のようなエラーが出る

raise NotFoundError('no lapack/blas resources found') numpy.distutils.system_info.NotFoundError: no lapack/blas resources found

lapackがないと怒られたが,この辺の依存関係を自分で解決しなきゃならないのかなと思うと荷が重い.

そこで調べてみるとpipでなく,apt-getでもインストールができる模様なので,この際よく使うものをまとめてインストールしておいた.なお,必要ならpython-devを入れておくとともに,依存関係ありそうなものも合わせてインストールした.

$ sudo apt-get install python python-dev

$ sudo apt-get install libpng-dev libjpeg8-dev libfreetype6-dev

$ sudo apt-get install python-numpy python-scipy python-matplotlib

# さらにハマる apt-getで入れてみたけど下記のエラーが出た

NameError: global name 'matplotlib' is not defined

そこでもう一度pipで入れてみる

$ sudo pip install matplotlib

The directory '/home/(user)/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/home/(user)/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Requirement already satisfied (use --upgrade to upgrade): matplotlib in /usr/local/lib/python2.7/dist-packages Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): numpy>=1.6 in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): pytz in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): pyparsing!=2.0.0,!=2.0.4,!=2.1.2,>=1.5.6 in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): cycler in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /usr/local/lib/python2.7/dist-packages (from python-dateutil->matplotlib) /usr/local/lib/python2.7/dist-packages/pip/vendor/requests/packages/urllib3/util/ssl.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning. SNIMissingWarning /usr/local/lib/python2.7/dist-packages/pip/vendor/requests/packages/urllib3/util/ssl.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.

言われるがままにオプションつけてみる

$ sudo -H pip install matplotlib

Requirement already satisfied (use --upgrade to upgrade): matplotlib in /usr/local/lib/python2.7/dist-packages Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): numpy>=1.6 in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): pytz in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): pyparsing!=2.0.0,!=2.0.4,!=2.1.2,>=1.5.6 in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): cycler in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /usr/local/lib/python2.7/dist-packages (from python-dateutil->matplotlib)

エラーは減った気がするので一旦ここでアンインストール

$ sudo -H pip uninstall matplotlib

アンインストールできたようなので再インストール

$ sudo -H pip install matplotlib

ログの内容は変わったけどSuccessfullyの文字が見えない

Requirement already satisfied (use --upgrade to upgrade): matplotlib in /usr/lib/pymodules/python2.7 Requirement already satisfied (use --upgrade to upgrade): numpy>=1.5 in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python2.7/dist-packages (from matplotlib) Collecting tornado (from matplotlib) /usr/local/lib/python2.7/dist-packages/pip/vendor/requests/packages/urllib3/util/ssl.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning. SNIMissingWarning /usr/local/lib/python2.7/dist-packages/pip/vendor/requests/packages/urllib3/util/ssl.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. InsecurePlatformWarning Downloading tornado-4.4.1.tar.gz (456kB) 100% |████████████████████████████████| 460kB 744kB/s Requirement already satisfied (use --upgrade to upgrade): pyparsing>=1.5.6 in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): nose in /usr/local/lib/python2.7/dist-packages (from matplotlib) Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /usr/local/lib/python2.7/dist-packages (from python-dateutil->matplotlib) Collecting backports.sslmatchhostname (from tornado->matplotlib) Downloading backports.sslmatchhostname-3.5.0.1.tar.gz Collecting singledispatch (from tornado->matplotlib) Downloading singledispatch-3.4.0.3-py2.py3-none-any.whl Collecting certifi (from tornado->matplotlib) Downloading certifi-2016.8.8-py2.py3-none-any.whl (384kB) 100% |████████████████████████████████| 389kB 686kB/s Collecting backportsabc>=0.4 (from tornado->matplotlib) Downloading backportsabc-0.4-py2.py3-none-any.whl Building wheels for collected packages: tornado, backports.ssl-match-hostname Running setup.py bdistwheel for tornado ... done Stored in directory: /root/.cache/pip/wheels/c2/65/99/727ea73b981e0ef1fb8ab678ecf86f39c809230f5301ac3a40 Running setup.py bdistwheel for backports.ssl-match-hostname ... done Stored in directory: /root/.cache/pip/wheels/5d/72/36/b2a31507b613967b728edc33378a5ff2ada0f62855b93c5ae1 Successfully built tornado backports.ssl-match-hostname Installing collected packages: backports.ssl-match-hostname, singledispatch, certifi, backports-abc, tornado Successfully installed backports-abc-0.4 backports.ssl-match-hostname-3.5.0.1 certifi-2016.8.8 singledispatch-3.4.0.3 tornado-4.4.1

実行してみるとやっぱり参照できてない旨のエラーが出る

NameError: global name 'matplotlib' is not defined

困ったので再度apt-getでupgradeかけてみる

sudo apt-get upgrade python-matplotlib

もう全部再インストールしたれ

sudo -H pip install numpy python-dateutil pytz pyparsing six --force-reinstall --upgrade sudo -H pip install matplotlib --force-reinstall --upgrade

Collecting matplotlib /usr/local/lib/python2.7/dist-packages/pip/vendor/requests/packages/urllib3/util/ssl.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning. SNIMissingWarning /usr/local/lib/python2.7/dist-packages/pip/vendor/requests/packages/urllib3/util/ssl.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. InsecurePlatformWarning Downloading matplotlib-1.5.2-cp27-cp27mu-manylinux1i686.whl (13.4MB) 100% |████████████████████████████████| 13.4MB 56kB/s Collecting python-dateutil (from matplotlib) Using cached pythondateutil-2.5.3-py2.py3-none-any.whl Collecting numpy>=1.6 (from matplotlib) Using cached numpy-1.11.1-cp27-cp27mu-manylinux1_i686.whl Collecting pytz (from matplotlib) Using cached pytz-2016.6.1-py2.py3-none-any.whl Collecting pyparsing!=2.0.0,!=2.0.4,!=2.1.2,>=1.5.6 (from matplotlib) Using cached pyparsing-2.1.8-py2.py3-none-any.whl Collecting cycler (from matplotlib) Downloading cycler-0.10.0-py2.py3-none-any.whl Collecting six>=1.5 (from python-dateutil->matplotlib) Using cached six-1.10.0-py2.py3-none-any.whl Installing collected packages: six, python-dateutil, numpy, pytz, pyparsing, cycler, matplotlib Found existing installation: six 1.10.0 Uninstalling six-1.10.0: Successfully uninstalled six-1.10.0 Found existing installation: python-dateutil 2.5.3 Uninstalling python-dateutil-2.5.3: Successfully uninstalled python-dateutil-2.5.3 Found existing installation: numpy 1.11.1 Uninstalling numpy-1.11.1: Successfully uninstalled numpy-1.11.1 Found existing installation: pytz 2016.6.1 Uninstalling pytz-2016.6.1: Successfully uninstalled pytz-2016.6.1 Found existing installation: pyparsing 2.1.8 Uninstalling pyparsing-2.1.8: Successfully uninstalled pyparsing-2.1.8 Found existing installation: cycler 0.10.0 Uninstalling cycler-0.10.0: Successfully uninstalled cycler-0.10.0 Successfully installed cycler-0.10.0 matplotlib-1.5.2 numpy-1.11.1 pyparsing-2.1.8 python-dateutil-2.5.3 pytz-2016.6.1 six-1.10.0

"Successfully installed" が出たので,試してみるにimportはできるのでインストールはできた模様

python import matplotlib

今度は実行時のエラーが出る

$ python plot3d.py /usr/local/lib/python2.7/dist-packages/matplotlib/fontmanager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment. warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.') Traceback (most recent call last): File "plot3d.py", line 55, in Plot() File "plot3d.py", line 31, in Plot fig = plt.figure() File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 527, in figure **kwargs) File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backendtkagg.py", line 84, in newfiguremanager return newfiguremanagergivenfigure(num, figure) File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backendtkagg.py", line 92, in newfiguremanagergiven_figure window = Tk.Tk() File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in init self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: no display name and no $DISPLAY environment variable

ひとまずインストールはできて,実行時のエラーは別の問題の模様なので終了→matplotlibで$DISPLAYが未定義の際に怒られた場合の対応

コメント(0)

コメントを投稿する