48

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

Codieroot
  • 697
  • 2
  • 7
  • 15

12 Answers12

80

Ahhh the classic lsb_release issue. I have battled this problem many times. The issue is that your default Python implementation is trying to use Python 3 but lsb_release requires Python 2. To fix this problem do the following:

  • Open /usr/bin/lsb_release (Make sure you use sudo or open as root!)
  • Edit the first line to be #! /usr/bin/python2.7
  • Save the file

now you can use pip again and everything should be fine.

UPDATE May 2019: Newer versions of Linux are shipping with Python3 by default. As noted in the comments here, you may just need to use a specific version of Python 3.

Nick Chapman
  • 4,402
  • 1
  • 27
  • 41
  • Should I need to restart they system after making changes? – Codieroot Jul 07 '17 at 09:58
  • @Codieroot nope, just save the file and you should be good to go. – Nick Chapman Jul 07 '17 at 09:59
  • @Codieroot the first line of your lsb_release file is now `#! /usr/bin/python2.7` and just that right? – Nick Chapman Jul 10 '17 at 13:17
  • Solved for me. If unsure of what your python installs are, check `/usr/bin` and `/usr/local/bin` to find what you have installed. On my system, that was 2.7 in usr/bin and 3.6 in usr/local/bin. Python was pointing at the latter. – Mike P. May 17 '18 at 18:03
  • just in my case on debian it was /usr/bin/python3.5 (/usr/bin/python2.7 was giving error about syntax) – nazar kuliyev May 08 '19 at 04:07
  • 12
    Just upgraded python3.6 to python3.7 and this issue occured. I edited the first line from `#! /usr/bin/python3` to `#! /usr/bin/python3.7` and everything was fine. Dont know why – WW00WW Jul 31 '19 at 01:42
  • When building Python from source, using make altinstall (to not override the default python) fixed my problem. – Meiswjn Nov 11 '20 at 11:29
  • I also installed 3.9 on my new Raspberry Pi (came with 2.7 & 3.7) from source and had to change the version. Very similar to @WW00WW's commend above, but in my case I changed it from `.../python3` to `.../python3.9` as that was the version I installed. – kirypto Jul 31 '21 at 01:39
  • I read that `lsb_release` is not installed anymore from `python3.6` onwards. So why changing the first line of the lsb_release bin file if the programme is not on your system? – Timo Sep 23 '21 at 09:20
51

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.

ibarrond
  • 6,617
  • 4
  • 26
  • 45
Jerry Isdale
  • 631
  • 5
  • 2
26

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.

Cleber Jorge Amaral
  • 1,316
  • 13
  • 26
6

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.

5

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

Vikram Hosakote
  • 3,528
  • 12
  • 23
2

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'.

Steven Kalt
  • 1,116
  • 15
  • 25
2

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.

MarkSkayff
  • 1,334
  • 9
  • 14
  • I put python3.8 on the shebang and get `ModuleNotFoundError: No module named 'lsb_release'` with `lsb_release`, `/usr/bin/python3.8` works. – Timo Sep 23 '21 at 09:14
2

Running sudo apt reinstall lsb-release fixed this for me. Credit to tony on this post

joshlsullivan
  • 1,375
  • 2
  • 14
  • 21
1

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
raratiru
  • 8,748
  • 4
  • 73
  • 113
0

I think this is a problem with old compiled python files that clash with an Ubuntu upgrade.

To me, the solution was to delete the following .pyc files

sudo rm /usr/lib/python2.7/dist-packages/*.pyc
0

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
hobs
  • 18,473
  • 10
  • 83
  • 106
0

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)


Brian de Alwis
  • 2,814
  • 2
  • 18
  • 32