PR

pipコマンドでlsb_releaseのエラーが発生した場合の対処

Python

Ubuntuにてpipを実行すると「subprocess.CalledProcessError: Command ‘(‘lsb_release’, ‘-a’)’ returned non-zero exit status 1.」と言われたので対処した方法を残しておきます。

$ python3 -m pip list
Package         Version
--------------- --------
boto3           1.17.15
botocore        1.20.15
jmespath        0.10.0
mysqlclient     2.0.3
numpy           1.20.1
opencv-python   4.5.1.48
pip             20.2.3
python-dateutil 2.8.1
s3transfer      0.3.4
setuptools      49.2.1
six             1.15.0
urllib3         1.26.3
Traceback (most recent call last):
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/__main__.py", line 26, in <module>
    sys.exit(_main())
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_internal/cli/main.py", line 75, in main
    return command.main(cmd_args)
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 121, in main
    return self._main(args)
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 265, in _main
    self.handle_pip_version_check(options)
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 149, in handle_pip_version_check
    session = self._build_session(
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 90, in _build_session
    session = PipSession(
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_internal/network/session.py", line 249, in __init__
    self.headers["User-Agent"] = user_agent()
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_internal/network/session.py", line 132, in user_agent
    zip(["name", "version", "id"], distro.linux_distribution()),
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_vendor/distro.py", line 125, in linux_distribution
    return _distro.linux_distribution(full_distribution_name)
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_vendor/distro.py", line 681, in linux_distribution
    self.version(),
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_vendor/distro.py", line 741, in version
    self.lsb_release_attr('release'),
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_vendor/distro.py", line 903, in lsb_release_attr
    return self._lsb_release_info.get(attribute, '')
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_vendor/distro.py", line 556, in __get__
    ret = obj.__dict__[self._fname] = self._f(obj)
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/site-packages/pip/_vendor/distro.py", line 1014, in _lsb_release_info
    stdout = subprocess.check_output(cmd, stderr=devnull)
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/subprocess.py", line 415, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/usr/local/pyenv/versions/3.8.8/lib/python3.8/subprocess.py", line 516, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.

解決方法

「/usr/bin/lsb_release」に記載されているシバン(Shebang)の部分が使用しているpython部分がうまく実行できずエラーが発生しているようです。
そのため1行目の部分を現在使用しているpythonに変更します。
自分の環境ではpyenvを使用しているためpython3をそのまま指定しました。
#!/usr/bin/python3 -Es

#!python3

再実行

再度pipを実行すると・・・

$ python3 -m pip list
Package         Version
--------------- --------
boto3           1.17.15
botocore        1.20.15
jmespath        0.10.0
mysqlclient     2.0.3
numpy           1.20.1
opencv-python   4.5.1.48
pip             20.2.3
python-dateutil 2.8.1
s3transfer      0.3.4
setuptools      49.2.1
six             1.15.0
urllib3         1.26.3

エラーが出なくなりました。

タイトルとURLをコピーしました