Python ライブラリ PycURL のアップデートに失敗したのでなんとかする話

Linux,Mac,プログラムPip,PycURL,Python,アップデート,ライブラリ

Python のライブラリ管理に pip を使っています。先日 pip-review コマンドを使ってアップデートをかけようとしたところ pycurl==7.43.0.6 から 7.45.1 へのアップデートでエラーを吐かれてしまいました。対処方法をメモしておきます。

エラーは 2 回に分けてでてきました(最初のエラーを解消したら次のエラーがでてきた)。

第一のエラー

まずアップデートコマンドを以下のように実行しました。

pip install -U pycurl

最初に怒られたエラーはこれです。

  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [27 lines of output]
      /tmp/pip-install-pjs7ejxj/pycurl_efc24bcd3e2546e79703379770e88720/setup.py:957: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
        if LooseVersion(distutils.__version__) > LooseVersion("1.0.1"):
      /tmp/pip-install-pjs7ejxj/pycurl_efc24bcd3e2546e79703379770e88720/setup.py:959: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
        if LooseVersion(distutils.__version__) < LooseVersion("1.0.3"):
      Traceback (most recent call last):
        File "/tmp/pip-install-pjs7ejxj/pycurl_efc24bcd3e2546e79703379770e88720/setup.py", line 235, in configure_unix
          p = subprocess.Popen((self.curl_config(), '--version'),
        File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
          self._execute_child(args, executable, preexec_fn, close_fds,
        File "/usr/lib/python3.9/subprocess.py", line 1823, in _execute_child
          raise child_exception_type(errno_num, err_msg, err_filename)
      FileNotFoundError: [Errno 2] No such file or directory: 'curl-config'

..."中略"...

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

肝は中略直前の “FileNotFoundError: [Errno 2] No such file or directory: 'curl-config’" ですね。

どうやら curl 関連が足りないようなので、関連パッケージをインストールしました。

sudo apt install libcurl4-openssl-dev libssl-dev

libcurl4-openssl-devlibssl-dev をインストールすることでエラーは解消されました。

第二のエラー

次に怒られたエラーはこれです。

Building wheels for collected packages: pycurl
  Building wheel for pycurl (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [24 lines of output]
      /tmp/pip-install-2h0mpzyy/pycurl_eac392a63c2b4b6ebcd5d4452987b8ef/setup.py:957: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
        if LooseVersion(distutils.__version__) > LooseVersion("1.0.1"):
      /tmp/pip-install-2h0mpzyy/pycurl_eac392a63c2b4b6ebcd5d4452987b8ef/setup.py:959: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
        if LooseVersion(distutils.__version__) < LooseVersion("1.0.3"):
      Using curl-config (libcurl 7.74.0)
      Using SSL library: OpenSSL/LibreSSL/BoringSSL
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-3.9
      creating build/lib.linux-x86_64-3.9/curl
      copying python/curl/__init__.py -> build/lib.linux-x86_64-3.9/curl
      running build_ext
      building 'pycurl' extension
      creating build/temp.linux-x86_64-3.9
      creating build/temp.linux-x86_64-3.9/src
      x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -ffile-prefix-map=/build/python3.9-RNBry6/python3.9-3.9.2=. -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -fPIC -DPYCURL_VERSION=\"7.45.1\" -DHAVE_CURL_SSL=1 -DHAVE_CURL_OPENSSL=1 -DHAVE_CURL_SSL=1 -I/usr/include/python3.9 -c src/docstrings.c -o build/temp.linux-x86_64-3.9/src/docstrings.o
      In file included from src/docstrings.c:4:
      src/pycurl.h:5:10: fatal error: Python.h: そのようなファイルやディレクトリはありません
          5 | #include <Python.h>
            |          ^~~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for pycurl

ここで肝は “error: command '/usr/bin/x86_64-linux-gnu-gcc’ failed with exit code 1" だと思われます。

gnu-gcc がないと怒っているので、ビルドに必要な最低限のパッケージが見つからないようです。(直感で gcc;Cコンパイラ を入れようとしましたが、すでに入っていました。)

実際には追加で次のパッケージをインストールしました。

sudo apt install python3-dev

python3-dev です。この後に再び pycurl のアップデートを実行すると、問題なく完了しました。

参考資料