pip install errors out: SyntaxError: invalid syntax

11,638

Solution 1

The problem is your version of pip is broken with Python 2.6. If you upgrade to 9.0.3 it should work again.

pip install pip==9.0.3

If you are unable to upgrade pip using pip, you could re-install the package as well using your local package manager, and then upgrade to pip 9.0.3.

Keep in mind that if you are using virtual environments it is recommended that you upgrade virtualenv as well so that your virtual environments will have pip 9.0.3 as well.

pip install virtualenv==15.2.0

Be careful though to not upgrade to a version of pip higher than 9.0.3 or virtualenv higher than 15.2.0 as Python 2.6 support was removed with those versions, as mentioned by Prateek.

Solution 2

pip works with CPython versions 2.7, 3.3, 3.4, 3.5, 3.6 and also pypy.

This means pip works on the latest patch version of each of these minor versions. Previous patch versions are supported on a best effort approach.

Just use below command one you upgrade to compatible Python version.

pip install requests

check pip-documentation for more details.

Including @eandersson's comments

Or you you can upgrade pip to 9.0.3

pip install pip==9.0.3

Share:
11,638
Anjana Shivangi
Author by

Anjana Shivangi

Updated on June 05, 2022

Comments

  • Anjana Shivangi
    Anjana Shivangi almost 2 years

    pip install does not work when trying to install virtualenv, requests or pex on CentOS6. I am on python2.6 and pip 9.0.1. Can anyone tell me why is this happening?

    (pex_build)[root@pex pex_build]# pip install virtualenv
    

    Output:

    Traceback (most recent call last):
      File "/opt/pex_build/bin/pip", line 7, in <module>
        from pip._internal import main
      File "/opt/pex_build/lib/python2.6/site-packages/pip/_internal/__init__.py", line 42, in <module>
        from pip._internal import cmdoptions
      File "/opt/pex_build/lib/python2.6/site-packages/pip/_internal/cmdoptions.py", line 16, in <module>
        from pip._internal.index import (
      File "/opt/pex_build/lib/python2.6/site-packages/pip/_internal/index.py", line 526
        {str(c.version) for c in all_candidates},
                          ^
    SyntaxError: invalid syntax
    

    Command:

    (pex_build) [root@pex pex_build]# pip install requests pex
    

    Output:

    Traceback (most recent call last):
      File "/opt/pex_build/bin/pip", line 7, in <module>
        from pip._internal import main
      File "/opt/pex_build/lib/python2.6/site-packages/pip/_internal/__init__.py", line 42, in <module>
        from pip._internal import cmdoptions
      File "/opt/pex_build/lib/python2.6/site-packages/pip/_internal/cmdoptions.py", line 16, in <module>
        from pip._internal.index import (
      File "/opt/pex_build/lib/python2.6/site-packages/pip/_internal/index.py", line 526
        {str(c.version) for c in all_candidates},
                          ^
    SyntaxError: invalid syntax
    

    Also curl gives the similar error, when trying to get get-pip.py

    Command:

    (pex_build) [root@pex pex_build]# curl https://bootstrap.pypa.io/get-pip.py | python
    
    
    
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 1602k  100 1602k    0     0  7373k      0 --:--:-- --:--:-- --:--:-- 14.6M
    
    Traceback (most recent call last):
      File "<stdin>", line 20636, in <module>
      File "<stdin>", line 197, in main
      File "<stdin>", line 82, in bootstrap
      File "/tmp/tmp5zrn_f/pip.zip/pip/_internal/__init__.py", line 42, in <module>
      File "/tmp/tmp5zrn_f/pip.zip/pip/_internal/cmdoptions.py", line 16, in <module>
      File "/tmp/tmp5zrn_f/pip.zip/pip/_internal/index.py", line 526
        {str(c.version) for c in all_candidates},
                          ^
    SyntaxError: invalid syntax
    
  • eandersson
    eandersson about 6 years
    This is incorrect. He mentioned that he using pip 9.0.1 which still supports Python 2.6. Pip 10 however does not. Might be worth including instructions on how to correctly identify the version he is running and how to downgrade.
  • eandersson
    eandersson about 6 years
    Actually, this is just a bug in pip 9.0.1. It's fixed in pip 9.0.3.