8

OS: Debian 8

Python: v3.4.2 (uninstalled v2.7 and v3.5 in order to reduce possible root causes)

I installed pip for Python 3 as per documentation:

sudo apt install python3-venv python3-pip

Afterwards, checking the version results in a SyntaxError:

$ python3 -m pip --version
Traceback (most recent call last):
  File "/usr/lib/python3.4/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.4/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.4/dist-packages/pip/__main__.py", line 21, in <module>
    from pip._internal.cli.main import main as _main
  File "/usr/local/lib/python3.4/dist-packages/pip/_internal/cli/main.py", line 60
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax

Please note the path to Python version 3.4, while the following check points to version 3.5:

$ pip --version
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 7, in <module>
    from pip._internal.cli.main import main
  File "/usr/local/lib/python3.5/site-packages/pip/_internal/cli/main.py", line 60
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax

However, which pip points to another path:

$ which pip
/usr/local/bin/pip

What is going wrong? How can I fix PIP?

I need it to install the tool elastalert.

What I have tried so far:

  1. Uninstalled PIP for Python 3.5 and checked version again.
  2. Uninstalled PIP for Python 2.7 and checked version again.
  3. Uninstalled Python v3.5 (apt remove --purge) and checked version again.
  4. Uninstalled Pyton v2.7 and checked version again.
  5. Uninstalled Python v3.4 and checked version again.
  6. Re-installed Pyhton v3.4 and checked version again.
3
  • 1
    Only Ubuntu and official flavors of Ubuntu (ubuntu.com/download/flavours) are on-topic here, refer to askubuntu.com/help/on-topic where you'll find other SE sites where you question will be welcome.
    – David
    Feb 18, 2021 at 13:21
  • @David OK, thanks!
    – fuggi
    Feb 18, 2021 at 13:28
  • Also, you cannot uninstall the default Python versions.
    – Eagle_Mike
    Feb 18, 2021 at 17:52

1 Answer 1

16

According to phd's post, the root cause is the PIP version installed by default being not compatible with the old Python version 3.4. The crucial clue was the f"" being only supported by Python v3.6+.

So I've uninstalled PIP and re-installed PIP 19.1.1 as follows:

  1. Uninstall incompatible PIP provided by distro repository:

    sudo apt remove --purge python3-pip
    
  2. Download a get-pip.py version which is compatible with my Python version 3.4:

    curl -O https://bootstrap.pypa.io/pip/3.4/get-pip.py
    
  3. Install PIP (in my case with sudo as global installation and with -E to apply proxy settings to sudo session):

    sudo -E python3 get-pip.py
    
  4. Optional: Upgrade PIP to ensure it's the latest available version:

    sudo -E python3 -m pip install --upgrade "pip < 19.2"
    
  5. Check version:

    python3 -m pip --version
    

    Ouput:

    pip 19.1.1 from /usr/local/lib/python3.4/dist-packages/pip (python 3.4)
    
2
  • 1
    OMG finally. I have read dozens of answers and none solved this problem but your answer did. Thank you!
    – Rick
    May 5, 2021 at 2:45
  • Excellent answer, really clear and it saved me today Feb 15 at 5:55

Not the answer you're looking for? Browse other questions tagged .