0
0

More than 3 years have passed since last update.

仮想環境構築でmatplotlibがインストール出来ない!

Last updated at Posted at 2019-12-23

matplotplibが入らない!!!

実務作業で仮想環境の構築が必要になった・・・でも、

pip install -r requirement.txt

でエラーログが出てくる...

概略

いろいろ試しましたが、結果的には、requirement.txtに入っているライブラリのバージョンと、インタプリタとして使ったpythonのバージョンが違っただけのことでした。

以下、自分の争いの痕跡です。

正当な解決方法はこちらに示しましたので、時間が無い方は先に見てください。

著者の環境

OS: Windows
CPU: Intel Cereron N4000 64bit
IDE: Pycharm
Python: 3.8 32bit版
仮想環境: venv

requirement.txtの中身

requirement.txt
appnope==0.1.0
attrs==19.2.0
backcall==0.1.0
bleach==3.1.0
boto3==1.9.250
botocore==1.12.250
cycler==0.10.0
decorator==4.4.0
defusedxml==0.6.0
docutils==0.15.2
entrypoints==0.3
importlib-metadata==0.23
ipykernel==5.1.2
ipython==7.8.0
ipython-genutils==0.2.0
ipywidgets==7.5.1
jedi==0.15.1
Jinja2==2.10.3
jmespath==0.9.4
jsonschema==3.1.1
jupyter==1.0.0
jupyter-client==5.3.4
jupyter-console==6.0.0
jupyter-core==4.6.0
kiwisolver==1.1.0
MarkupSafe==1.1.1
mistune==0.8.4
more-itertools==7.2.0
nbconvert==5.6.0
nbformat==4.4.0
notebook==6.0.1
numpy==1.17.2
pandas==0.25.1
pandocfilters==1.4.2
parso==0.5.1
patsy==0.5.1
pexpect==4.7.0
pickleshare==0.7.5
prometheus-client==0.7.1
prompt-toolkit==2.0.10
ptyprocess==0.6.0
Pygments==2.4.2
pyparsing==2.4.2
pyrsistent==0.15.4
python-dateutil==2.8.0
pytz==2019.3
pyzmq==18.1.0
qtconsole==4.5.5
s3transfer==0.2.1
scipy==1.3.1
Send2Trash==1.5.0
six==1.12.0
statsmodels==0.10.1
terminado==0.8.2
testpath==0.4.2
tornado==6.0.3
traitlets==4.3.3
urllib3==1.25.6
wcwidth==0.1.7
webencodings==0.5.1
widgetsnbextension==3.5.1
zipp==0.6.0
requests==2.22.0

エラー内容

エラー内容
# Extension modules only get added in build_ext, as numpy will have
    RuntimeError: Running cythonize failed!
    ================================================================================
    Edit setup.cfg to change the build options

    BUILDING MATPLOTLIB
      matplotlib: yes [3.1.1]
          python: yes [3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC
                      v.1916 32 bit (Intel)]]
        platform: yes [win32]

    OPTIONAL SUBPACKAGES
     sample_data: yes [installing]
           tests: no  [skipping due to configuration]

    OPTIONAL BACKEND EXTENSIONS
             agg: yes [installing]
           tkagg: yes [installing; run-time loading from Python Tcl/Tk]
          macosx: no  [Mac OS-X only]

    OPTIONAL PACKAGE DATA
            dlls: no  [skipping due to configuration]

    Cythonizing sources
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs f
or full command output.
WARNING: You are using pip version 19.2.3, however version 19.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

お決まりのpipアップグレードしてってやつと、どうやらMatplotlibがどうちゃらこうちゃら、よくわからん。。。

いろいろ調べたら、どうやら.whlファイルってのが存在していないらしい(参考:Install Matplotlib on Windows10 machines)

リンク先の解法では、pip install nameof.whlで解決できるよって書いてあったけど、業務上勝手に仮想環境を変えるのはよろしくない。。。

ここで筆者環境にも書いたけど、pythonの32bit版を入れていたことに気づいた。本家インストーラーを使うと、32bit版が勝手にインストールされるらしい。32bitの方が安定らしい(?)けど、64bit版を要求するライブラリもあるらしい(?)(参考:Pythonの32ビット版と64ビット版について(Windows))

なので、急遽pythonを64bit版の3.8にしてみることにした

試してみたこと1:pythonを64bit版にする。

公式サイトインストーラーから、pythonの64bit版、バージョンは、3.8をダウンロード。32bit版はアンインストールし、さらにPycharmのインタープリターも、64bit版に変更。

結果1:pythonを64bit版にしてみる。

効果なし。エラー内容に変化がなかった。

試してみたこと2:pipをアップグレードする。

python -m pip install --upgrade pipでpipのバージョンを最新のものにした。

結果2:pipをアップグレードする。

効果なし。

エラー内容
Collecting matplotlib==3.1.1
  Using cached https://files.pythonhosted.org/packages/12/d1/7b12cd79c791348cb0c78ce6e7d
16bd72992f13c9f1e8e43d2725a6d8adf/matplotlib-3.1.1.tar.gz
    ERROR: Command errored out with exit status 1:
     command: 'c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\scripts\python.exe
' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\masak\\AppData\\Lo
cal\\Temp\\pip-install-xzd8e765\\matplotlib\\setup.py'"'"'; __file__='"'"'C:\\Users\\mas
ak\\AppData\\Local\\Temp\\pip-install-xzd8e765\\matplotlib\\setup.py'"'"';f=getattr(toke
nize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"'
);f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users
\masak\AppData\Local\Temp\pip-install-xzd8e765\matplotlib\pip-egg-info'
         cwd: C:\Users\masak\AppData\Local\Temp\pip-install-xzd8e765\matplotlib\
    Complete output (118 lines):
    Processing numpy/random\_bounded_integers.pxd.in
    Processing numpy/random\mtrand.pyx
    Traceback (most recent call last):
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\tools\c
ythonize.py", line 61, in process_pyx
        from Cython.Compiler.Version import version as cython_version
    ModuleNotFoundError: No module named 'Cython'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\tools\c
ythonize.py", line 238, in <module>
        main()
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\tools\c
ythonize.py", line 234, in main
        find_process_files(root_dir)
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\tools\c
ythonize.py", line 225, in find_process_files
        process(root_dir, fromfile, tofile, function, hash_db)
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\tools\c
ythonize.py", line 191, in process
        processor_function(fromfile, tofile)
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\tools\c
ythonize.py", line 66, in process_pyx
        raise OSError('Cython needs to be installed in Python as a module')
    OSError: Cython needs to be installed in Python as a module
    Running from numpy source directory.
    C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\setup.py:425: U
serWarning: Unrecognized setuptools command, proceeding with generating Cython sources a
nd expanding templates
      run_build = parse_setuppy_commands()
    Traceback (most recent call last):
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 154, in save_modules
        yield saved
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 195, in setup_context
        yield
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 250, in run_setup
        _execfile(setup_script, ns)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 45, in _execfile
        exec(code, globals, locals)
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\setup.p
y", line 450, in <module>
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\setup.p
y", line 433, in setup_package
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\setup.p
y", line 240, in generate_cython
        # Extension modules only get added in build_ext, as numpy will have
    RuntimeError: Running cythonize failed!

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\masak\AppData\Local\Temp\pip-install-xzd8e765\matplotlib\setup.py",
 line 262, in <module>
        setup(
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\__init__.py", line 144, in setup
        _install_setup_requires(attrs)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\__init__.py", line 139, in _install_setup_requires
        dist.fetch_build_eggs(dist.setup_requires)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\dist.py", line 716, in fetch_build_eggs
        resolved_dists = pkg_resources.working_set.resolve(
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\pkg
_resources\__init__.py", line 780, in resolve
        dist = best[req.key] = env.best_match(
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\pkg
_resources\__init__.py", line 1065, in best_match
        return self.obtain(req, installer)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\pkg
_resources\__init__.py", line 1077, in obtain
        return installer(requirement)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\dist.py", line 786, in fetch_build_egg
        return cmd.easy_install(req)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\command\easy_install.py", line 679, in easy_install
        return self.install_item(spec, dist.location, tmpdir, deps)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\command\easy_install.py", line 705, in install_item
        dists = self.install_eggs(spec, download, tmpdir)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\command\easy_install.py", line 890, in install_eggs
        return self.build_and_install(setup_script, setup_base)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\command\easy_install.py", line 1158, in build_and_install
        self.run_setup(setup_script, setup_base, args)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\command\easy_install.py", line 1144, in run_setup
        run_setup(setup_script, args)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 253, in run_setup
        raise
      File "C:\Users\masak\AppData\Local\Programs\Python\Python38\lib\contextlib.py", li
ne 131, in __exit__
        self.gen.throw(type, value, traceback)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 195, in setup_context
        yield
      File "C:\Users\masak\AppData\Local\Programs\Python\Python38\lib\contextlib.py", li
ne 131, in __exit__
        self.gen.throw(type, value, traceback)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 166, in save_modules
        saved_exc.resume()
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 141, in resume
        six.reraise(type, exc, self._tb)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\_vendor\six.py", line 685, in reraise
        raise value.with_traceback(tb)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 154, in save_modules
        yield saved
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 195, in setup_context
        yield
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 250, in run_setup
        _execfile(setup_script, ns)
      File "c:\users\masak\pycharmprojects\hotelpricing\venv_hotel\lib\site-packages\set
uptools\sandbox.py", line 45, in _execfile
        exec(code, globals, locals)
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\setup.p
y", line 450, in <module>
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\setup.p
y", line 433, in setup_package
      File "C:\Users\masak\AppData\Local\Temp\easy_install-_r6w5bln\numpy-1.18.0\setup.p
y", line 240, in generate_cython
        # Extension modules only get added in build_ext, as numpy will have
    RuntimeError: Running cythonize failed!
    ================================================================================
    Edit setup.cfg to change the build options

    BUILDING MATPLOTLIB
      matplotlib: yes [3.1.1]
          python: yes [3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC
                      v.1916 64 bit (AMD64)]]
        platform: yes [win32]

    OPTIONAL SUBPACKAGES
     sample_data: yes [installing]
           tests: no  [skipping due to configuration]

    OPTIONAL BACKEND EXTENSIONS
             agg: yes [installing]
           tkagg: yes [installing; run-time loading from Python Tcl/Tk]
          macosx: no  [Mac OS-X only]

    OPTIONAL PACKAGE DATA
            dlls: no  [skipping due to configuration]

    Cythonizing sources
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs f
or full command output.

特に変化もなかったです。

試した見たこと3:WSLでpyenvを使ってPython3.6.6をインストール

Windows SubSystem for Linuxと呼ばれる機能を使って、pyenvをインストールしました。pythonのバージョン管理のためのものです。このrequirement.txtはpython3.6.6で作られたらしいので、それでインストールしてみました

結果3:WSLでpyenvを使ってPython3.6.6をインストール

成功しました。でも、このままじゃPycharmを使った作業が出来ないです...

試してみたこと4:Python3.6.6と3.8.1をWindows内で共存、管理する

別の記事に書いたので、こちらを御覧ください

結果4:Python3.6.6と3.8.1をWindows内で共存、管理する

もちろん、成功しました。

考察

requirement.txtに乗っているライブラリのバージョンにあったpythonのバージョンを用いるべきだということですね。こんな初歩的なことだったとは...

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0