45

I had Python versions of 2.7 and 3.5. I wanted the install a newer version of Python which is python 3.8. I am using Ubuntu 16.04 and I can not just uninstall Python 3.5 due to the dependencies. So in order to run my scripts, I use python3.8 app.py. No problem so far. But when I want to install new packages via pip:

python3.8 -m pip install pylint

It throws an error:

AttributeError: module 'platform' has no attribute 'linux_distribution'

So far, I tried:

sudo update-alternatives --config python3

and chose python3.8 and run command by starting with python3 but no luck.

Then:

sudo ln -sf /usr/bin/python3.5 /usr/bin/python3

I also tried running the command by starting with python3 but it did not work either.

How can I fix it so that I can install new packages to my new version of Python?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
EmreAkkoc
  • 623
  • 1
  • 11
  • 18
  • 2
    It seems `pylint` is not yet updated to support Python3.8. – Gino Mempin Nov 07 '19 at 23:49
  • tried pillow, scipy, pandas, selenium non of them does. but installed python 3.7 and its all fine. Thank you. – EmreAkkoc Nov 08 '19 at 00:03
  • 9
    `platform.linux_distribution` was removed in Python 3.8. See: https://bugs.python.org/issue28167, which seems to affect the package setup/install scripts. You might have to report the issue to the respective packages (or submit a PR to fix it for them). – Gino Mempin Nov 08 '19 at 00:09
  • Related: https://github.com/PyCQA/pylint/issues/3291 – sinoroc Dec 10 '19 at 09:29

7 Answers7

59

It looks like at least on my Ubuntu 16.04, pip is shared for all Python versions in /usr/lib/python3/dist-packages/pip.

This is what I did to get it working again:

  • sudo apt remove python3-pip
  • sudo python3.8 -m easy_install pip

You might want to install the python 3.5 version of pip again with sudo python3.5 -m easy_install pip.

Dave Halter
  • 15,556
  • 13
  • 76
  • 103
  • 1
    Works great on ubuntu 16.04 + python3.8. Thanks! – Alexandr S. Mar 29 '20 at 10:30
  • 3
    If you don't already have "easy_install" on your system, you will want to run "sudo apt-get python3-setuptools" (for Ubuntu) – Dave Aug 03 '20 at 12:44
  • Thanks for your answer it help me to resolve my issue @Dave Halter – Damith Udayanga Mar 10 '21 at 10:02
  • 1
    If the command "sudo apt-get python3-setuptools" does not work, you should put "install" before python3-setuptools, like this: "sudo apt-get install python3-setuptools" – Straight Coding Jun 14 '21 at 01:15
  • 2
    This is very bad advice. Never sudo pip/easy install things: you mess up OS packages. – Federico Jun 24 '21 at 00:31
  • I agree that this is bad advice, but it's only bad advice, because the Python packaging is broken in some places. It makes no sense that there is a generic pip for all the different Python 3.x installations. – Dave Halter Jun 24 '21 at 17:16
  • Has some problem with this on docker for Ubuntu 16 via bitbucket pipeline but [below answer](https://stackoverflow.com/a/59936888/6705161) helped me. – dannydedog Aug 05 '21 at 14:43
37

Python 3.8 removed some stuff. I solved my problems with pip (specifically pip install) by installing pip with curl.

What worked for me was downloading get-pip.py and run it with Python 3.8:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.8 get-pip.py

Source: https://pip.pypa.io/en/stable/installing/

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Gur Telem
  • 706
  • 1
  • 7
  • 14
  • 1
    The accepted answer did not work for me, but this one did. Ubuntu 16.04 WSL. – Karl Nov 11 '21 at 14:04
  • 2
    You can make it in one line: `curl https://bootstrap.pypa.io/get-pip.py | python3.8` – Shoham Nov 25 '21 at 14:32
  • In my case, just for tests, I ended up installing a very old version of pip (pip 7.1.2). The install worked fine but I could not uninstall `pip` via `pip uninstall -y pip` since it was throwing the error OP had. I had to resort to `get-pip.py` to fix it. – vvvvv Feb 28 '23 at 07:37
15

The problem is that package.linux_distribution was deprecated starting with Python 3.5(?). and removed altogether for Python 3.8.

Use the distro package instead. This package only works on Linux however.

I ran into this problem after installing OpenCobolIDE on Linux Mint 20, having upgraded Python to the latest level. have submitted a code fix to the OpenCobolIDE author to review and test. I was able to get the IDE to start up and run with this fix.

Essentially the fix uses the distro package if available, otherwise it uses the old platform package. For example:

This code imports distro if available:

import platform
using_distro = False
try:
    import distro
    using_distro = True
except ImportError:
    pass

Then you can test the value of using_distro to determine whether to get the linux distro type from package or distro, for example:

if using_distro:
    linux_distro = distro.like()
else:
    linux_distro = platform.linux_distribution()[0]
modrobert
  • 19
  • 6
Mark Puddephat
  • 151
  • 1
  • 3
  • For me, executing this code leads to linux_distro being an empty string... :/ (Arch Linux on Mac Book Pro 2015) – pfincent Dec 21 '20 at 13:36
2

In my case, removing python-pip-whl package helped:

apt-get remove python-pip-whl

It removed also pip and virtualenv, so I had to install them again:

curl https://bootstrap.pypa.io/get-pip.py | python3
pip install virtualenv
DJ83
  • 67
  • 3
1

Check if your wheels installation is old. I was getting this same error and fixed it with

python3.8 -m pip install --upgrade pip setuptools wheel

Pylint seems to work on python3.8

Jack Thomson
  • 450
  • 4
  • 9
1

If you have this issue when running a docker-compose up command. The solutions above do not work. You should install docker ce (https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)

Eugene
  • 41
  • 2
  • 12
0

I recently had this error and it turns out that I had a package called platform at a folder on my path ahead of the standard library and so the interpreter imported that instead. Check your path to what it is that you're actually importing.

pol
  • 61
  • 1
  • 3