113

On a fresh Vagrant VM using box bento/centos-7 the following commands corrupt my pip installation:

yum update
yum install epel-release -y
yum install python-pip -y
/usr/bin/pip2 install --upgrade pip setuptools pyOpenSSL psycopg2-binary lxml

This fails at the end with

  Downloading https://files.pythonhosted.org/packages/84/48/5c99d8770fd0a9eb0f82654c3294aad6d0ba9f8600638c2e2ad74f2c5d52/setuptools-52.0.0.tar.gz (2.1MB)
    100% |████████████████████████████████| 2.1MB 821kB/s
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "setuptools/__init__.py", line 16, in <module>
        import setuptools.version
      File "setuptools/version.py", line 1, in <module>
        import pkg_resources
      File "pkg_resources/__init__.py", line 1367
        raise SyntaxError(e) from e
                                ^
    SyntaxError: invalid syntax

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-GZrC3W/setuptools/
You are using pip version 8.1.2, however version 21.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

If I then upgrade Pip I notice that my Python is broken. After that all pip commands end with SyntaxError:

[root@bkd-openam ~]# pip install --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/9e/24/bc928987f35dd0167f21b13a1777c21b9c5917c9894cff93f1c1a6cb8f3b/pip-21.0.tar.gz
Installing collected packages: pip
  Found existing installation: pip 8.1.2
    Uninstalling pip-8.1.2:
      Successfully uninstalled pip-8.1.2
  Running setup.py install for pip ... done
Successfully installed pip-21.0
[root@bkd-openam ~]# /usr/bin/pip2 install --upgrade pip setuptools pyOpenSSL psycopg2-binary lxml
Traceback (most recent call last):
  File "/usr/bin/pip2", line 9, in <module>
    load_entry_point('pip==21.0', 'console_scripts', 'pip2')()
  File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 378, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 2566, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 2260, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/usr/lib/python2.7/site-packages/pip/_internal/cli/main.py", line 60
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax

Why is this? Is this a known issue with Python? How do I fix this, without using virtual environments or upgrading the OS or Python version?

Can I use Pip version 20 in this Python installation? How do I prevent upgrade to Pip 21?


For the equivalent issue with installing pip in old Python installations, see Installing pip is not working in python < 3.6.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
onknows
  • 6,151
  • 12
  • 65
  • 109
  • 6
    pip doesn't support Python 2 anymore. Both errors are Python 3 syntax run with pip2. – 9769953 Jan 26 '21 at 05:36
  • Is there a specific reason you're using the `pip2` command? In general, I suggest installing pyenv, not using the package mangager's Python – OneCricketeer Jan 26 '21 at 05:47
  • 1
    @00 No, the 2nd error could be from Python 3.4 or 3.5. – phd Jan 26 '21 at 05:48
  • This is a known problem with too recent `pip` on older Pythons: see https://stackoverflow.com/a/65871131/7976758 – phd Jan 26 '21 at 05:49
  • @phd Except that other lines of the second traceback show python2.7 (as well as pip2). While you're perhaps technically correct in a broader sense (but what about Python 3.3 and before?), here it's clearly Python 3 code that is installed and run under Python 2. – 9769953 Jan 26 '21 at 12:33
  • @00 Python 3.3 and before were dropped even by me. The libraries I maintain currently support Python 2.7 and 3.4+. – phd Jan 26 '21 at 15:09
  • f-string was imported after Python 3.6. It seems the updated pip uses f-string while in fact the Python version cannot support f-string. So, are you using Python 3.x < 3.6? – GoingMyWay Jan 28 '21 at 07:50

12 Answers12

58

You can get an older version (2.7) of get-pip.py

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py

In my opinion, you should create a python2 virtualenv and install your python2 package in it, as well as pip2. For example, when I installed cuckoo sandbox, I did it like this:

virtualenv --python=python2 cuckoo/   # make your virtualenv folder with python2
cd cuckoo
source bin/activate
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py   # get pip for python2
python2 get-pip.py        # install pip in your virtualenv
pip install cuckoo       # install your python2 package

Hope it will help.

basketmaker
  • 581
  • 3
  • 5
  • 2
    The above curl command has changed to curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py – Jonathan_M Mar 10 '21 at 13:14
  • 1
    The URL has changed again to: `https://bootstrap.pypa.io/pip/2.7/get-pip.py`. And could you also add the command to install that: `python2 get-pip.py`. Worked to fix my M1 Mac broken pip install on the native python2.7 – yeliabsalohcin Mar 26 '21 at 09:12
46

This problem has to do with the fact that Python 2.7 reached end of its life and that the PIP community dropped support for it this month.

PIP displays deprecation notices such as

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.

To fix this and continue with unsupported Python 2.7 you should not upgrade to the latest pip version but to a version < 21.

pip install --upgrade "pip < 21.0"
onknows
  • 6,151
  • 12
  • 65
  • 109
  • 16
    Presumably this will not work correctly if `pip` has already been upgraded to a non-working version. – Ken Williams Feb 05 '21 at 16:04
  • 5
    I don't understand the issue. I have both python2.7 and python3.5 installed. Python2.7 is not "dead" but I digress... If PIP wants python3.5, then why is it trying to use python2.7, or why does it even care whether python2.7 exists on my system? PIP is supposedly written by versioning *experts*. – personal_cloud Mar 07 '21 at 23:28
41

As PIP dropped support for Python 2.7 in result we are facing the above mentioned issue, following are the commands which actually worked for me on Ubuntu.

sudo apt-get remove --purge python-pip
sudo apt-get autoremove
sudo rm -f /usr/local/bin/pip
sudo easy_install pip==20.3.4
pip --version

Output: pip 20.3.4

Same can be achieved for CentOS by changing package manager name.

gondaljutt
  • 681
  • 4
  • 15
9

Update: Please use following command to install the pip on python2.7

curl  https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
python get-pip.py

Enjoy it!

W. Dan
  • 906
  • 8
  • 10
7

This helped me (Ubuntu 16.04)

In general I removed completely pip and pip3 like:

whereis pip
sudo rm -f <results from whereis pip
whereis pip3
sudo rm -f <results from whereis pip3

Search pip and remove files and directories. In my case it was:

sudo rm -fr ~/.local/bin/pip3.5
sudo rm -fr ~/.local/bin/pip3
sudo rm -fr ~/.local/lib/python3.5/site-packages/pip*
sudo rm -rf ~/.cache/pip/
sudo rm -rf  /usr/lib/python2.7/dist-packages/pip*
sudo rm -rf  /usr/lib/python3/dist-packages/pip*

Then I installed pip3 once again:

Python 3.5

wget https://bootstrap.pypa.io/pip/3.5/get-pip.py

Python 3.6

wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py --ignore-installed
4

Its version conflict issue. first check python --version, below steps for python version 2.7

  1. curl https://bootstrap.pypa.io/2.7/get-pip.py -o get-pip.py
  2. python get-pip.py
  3. install virtual env: python -m pip install --user virtualenv
  4. create venv: python -m virtualenv env
  5. activate venv: source env/bin/activate
Dharman
  • 30,962
  • 25
  • 85
  • 135
4

When you tried to upgrade pip it was upgraded to version 21 and as written in the link: Python2-support

pip 20.3 was the last version of pip that supported Python 2. Bugs reported with pip which only occur on Python 2.7 will likely be closed as “won’t fix” issues by pip’s maintainers.

So now you first need to remove the current pip installed, which you can do from Centos 7:

yum remove python2-pip

Now we will install pip again

yum install python2-pip -y

Now we will upgrade pip to the supported version

pip install --upgrade pip==20.3.4

Now pip command will work fine

pip -V
Vedant Pareek
  • 391
  • 3
  • 9
  • 1
    Nowadays, running `pip install --upgrade pip` on a CentOS 7 system causes pip to break itself. Evidently this causes Python 2.x pip to download and install a broken version of pip that **won't run in Python 2**. This can be fixed using `yum reinstall python2-pip`. If you want to use the pip for Python 3.x, use `pip3` (from the `python3-pip` yum package). – amphetamachine May 13 '21 at 17:48
4

I'm reporting my solution because it worked for me on MacOS (not on Linux, which these other solutions were for). Hopefully this will help someone in case they have a problem similar on Mac. I'm running MacOS 11.4. PIP got broken on my system somehow. My global system is supposed to be using Python3 but I was getting the error message:

 Traceback (most recent call last):
  File "/usr/local/bin/pip", line 11, in <module>
    load_entry_point('pip==21.0.1', 'console_scripts', 'pip')()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2843, in load_entry_point
    return ep.load()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2434, in load
    return self.resolve()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2440, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/Library/Python/2.7/site-packages/pip-21.0.1-py2.7.egg/pip/_internal/cli/main.py", line 60
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax

My solution was running this:

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

Now PIP is working again on my system.

C.D.
  • 406
  • 1
  • 5
  • 11
2

If this logic is used in a (e.g.) Github Actions matrix of different Python versions, the following bash snippet will determine if pip should be updated before pip 21.0 or not. For instance, Python 3.5 should not update beyond pip 21.0, but Python 3.6 can use more recent versions.

use_pip_lt_21=`python -c "import sys; print(sys.version_info[:2] < (3, 6))"`
if [ $use_pip_lt_21 = "True" ]; then
  pip="pip < 21.0"
else
  pip="pip"
fi
pip install --disable-pip-version-check --upgrade "$pip"
Mike T
  • 41,085
  • 18
  • 152
  • 203
1

Environment

Ubuntu 16.04 LTS 32-bit.

Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-209-generic i686)

Python 3.5

I was getting the error running these commands:

$ sudo python3 -m venv /opt/certbot/
$ sudo /opt/certbot/bin/pip install --upgrade pip
$ sudo /opt/certbot/bin/pip install certbot

Traceback (most recent call last): File "/opt/certbot/bin/pip", line 7, in from pip._internal.cli.main import main File "/opt/certbot/lib/python3.5/site-packages/pip/_internal/cli/main.py", line 58 sys.stderr.write(f"ERROR: {exc}") ^ SyntaxError: invalid syntax

The python3 is just a symlink to python3.5:

/usr/bin/python3 -> python3.5

Python 3.9

Another error is thrown:

$ sudo python3.9 -m venv /opt/certbot/

Error: Command '['/opt/certbot/bin/python3.9', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

Solution: Python 3.6

These commands solved the problem for me:

$ sudo python3.6 -m venv /opt/certbot/
$ sudo /opt/certbot/bin/pip install --upgrade pip
$ sudo /opt/certbot/bin/pip install certbot
Megidd
  • 7,089
  • 6
  • 65
  • 142
-2

these commands solved for me on ubuntu 20.04 LTS:

$ sudo apt update
$ sudo apt upgrade
$ sudo add-apt-repository universe
$ sudo apt install python2
$ curl https://bootstrap.pypa.io/2.7/get-pip.py | sudo python
$$ pip --version

summarized from https://linuxhint.com/install_python_pip_tool_ubuntu/ and https://unix.stackexchange.com/a/631283 by editin 5th command

Vahab
  • 307
  • 4
  • 7
-2

remove the pip and reinstall it, on ubantu16.04

sudo apt-get remove --purge python-pip
sudo apt-get autoremove
rm -r /home/xxx/.local/bin/pip
rm -r /usr/local/bin/pip
sudo apt update
curl https://bootstrap.pypa.io/2.7/get-pip.py | sudo python
sudo ln /usr/local/bin/pip /home/xxx/.local/bin/pip
haiboTang
  • 1
  • 1