pip is showing error 'lsb_release -a' returned non-zero exit status 1

Viewed 39445

I am trying to install packages using pip and it is throwing error.

Command that I have used,

sudo pip install selenium

The error it is showing,

Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 272, in run
    with self._build_session(options) as session:
  File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 72, in _build_session
    insecure_hosts=options.trusted_hosts,
  File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 329, in __init__
    self.headers["User-Agent"] = user_agent()
  File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 93, in user_agent
    from pip._vendor import distro
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/distro.py", line 1050, in <module>
    _distro = LinuxDistribution()
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/distro.py", line 594, in __init__
    if include_lsb else {}
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/distro.py", line 933, in _get_lsb_release_info
    raise subprocess.CalledProcessError(code, cmd, stdout)
CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/pip/__init__.py", line 233, in main
    return command.main(cmd_args)
  File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 251, in main
    timeout=min(5, options.timeout)) as session:
  File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 72, in _build_session
    insecure_hosts=options.trusted_hosts,
  File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 329, in __init__
    self.headers["User-Agent"] = user_agent()
  File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 93, in user_agent
    from pip._vendor import distro
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/distro.py", line 1050, in <module>
    _distro = LinuxDistribution()
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/distro.py", line 594, in __init__
    if include_lsb else {}
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/distro.py", line 933, in _get_lsb_release_info
    raise subprocess.CalledProcessError(code, cmd, stdout)
subprocess.CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1

I have searched everywhere, but there is no solution for this problem. It started behaving like this recently. Also, Software Updater stopped working. Not sure if it is related to that. I am using Ubuntu 14.04 LTS and python 2.X

11 Answers

After installing python3.7.3 and changing the symbolic link of python3 to point to it, I had this error. Fixed it by changing first line to:

# !/usr/bin/python3.5 -Es

The original was almost that... had to add the ".5" only.

In short, it was solved doing this:

$ sudo ln -s /usr/share/pyshared/lsb_release.py /usr/local/lib/python3.8/site-packages/lsb_release.py

Details:

When trying $ sudo pip3 install something I had the error referred in this thread:

... a long traceback, and ... 
subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.

It started to happen after updating from python 3.5 to 3.8. I have tried several solutions without success. A clue for the solution came when executing $ lsb_release -a which gave me the following error:

Traceback (most recent call last):
  File "/usr/bin/lsb_release", line 25, in <module>
    import lsb_release
ModuleNotFoundError: No module named 'lsb_release'

This other error message led me to this solution which is explaining that from version 3.6 python has no lsb_release.py file any more. The solution is just creating a link for this missing file.

I had the same problem on a shared hosting account which is very limiting (I was installing python and pip for my user only). Their lsb_release -a returns something non-standard and I cannot change it. I solved the issue by editing distro.py (in your case: /usr/local/lib/python2.7/dist-packages/pip/_vendor/distro.py) and changing the default parameter of __init__ method. In my version it was at the line 545. Snippet:

def __init__(self,
    include_lsb=True,
    os_release_file='',
    distro_release_file=''):

Just change include_lsb=True to include_lsb=False.

I was able to fix this error message with unset _PYTHON_SYSCONFIGDATA_NAME. This prevented lsb_release from trying to import $_PYTHON_SYSCONFIGDATA_NAME. After that, pip install worked again. This fix applies if you run lsb_release -a and get ModuleNotFoundError: No module named '_sysconfigdata_x86_64_conda_cos6_linux_gnu'.

The solution for this issue is to update the shebang on the /usr/bin/lsb_release

The shebang on this lsb_release file tells which version of python to use. This has to match with the current version of python you using.

For instance, I'm using Python 3.5 so I'd change the shebang to be:

#!/usr/bin/python3.5 -Es

And get it working.

After installing python from source on Debian-9 (I did install those packages however) I had the same issue. This is how I solved it:

/usr/local/bin/python3.x -m venv <my_venv>
. <my_venv>/bin/activate
easy_install pip
easy_install setuptools
easy_install wheel
pip install -U setuptools wheel pip

I had to uninstall python3-dev and python2.7-dev on a conflict between miniconda-installed python3.4 and my python3.6 compiled from source on a Raspberry pi (Raspbian Stretch with Arm71) and then reinstall pip and pip3:

sudo apt-get -y remove python2.7-dev python3-dev
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo python3 get-pip.py

I saw the same error and sudo rm /usr/bin/lsb_release resolved it for me.

The underlying problem is in pip's vendor/distro.py: it uses subprocess.check_output() to call lsb_release -a. subprocess.check_output() throws a subprocess.CalledProcessError on error, but the surrounding call is only catching OSError, so the CalledProcessError continues leading to failure. I saw a similar failure with the call to uname -rs. This error has recently been fixed upstream in distro.

Patching pip's vendored dependency is pretty simple:

--- /usr/local/lib/python2.7/site-packages/pip/_vendor/distro.py    2021-05-07 15:06:41.191965026 +0000
+++ /usr/local/lib/python2.7/site-packages/pip/_vendor/distro.py    2021-05-07 15:06:53.092831652 +0000
@@ -1010,7 +1010,7 @@
             try:
                 cmd = ('lsb_release', '-a')
                 stdout = subprocess.check_output(cmd, stderr=devnull)
-            except OSError:  # Command not found
+            except (OSError, subprocess.CalledProcessError):  # Command not found
                 return {}
         content = stdout.decode(sys.getfilesystemencoding()).splitlines()
         return self._parse_lsb_release_content(content)
@@ -1045,7 +1045,7 @@
             try:
                 cmd = ('uname', '-rs')
                 stdout = subprocess.check_output(cmd, stderr=devnull)
-            except OSError:
+            except (OSError, subprocess.CalledProcessError):  # Command not found
                 return {}
         content = stdout.decode(sys.getfilesystemencoding()).splitlines()
         return self._parse_uname_content(content)


Related