Skip to content

Fixing pip SyntaxError: sys.stderr.write(f"ERROR: {exc}") in Python 2.7

When working with Python 2.7 systems, you may encounter syntax errors when trying to use pip after an upgrade attempt:

python
File "/usr/lib/python2.7/site-packages/pip/_internal/cli/main.py", line 60
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax

This error occurs because Python 2.7 doesn't support f-strings, which were introduced in Python 3.6.

Root Cause: Pip Dropped Python 2.7 Support

The core issue is that pip version 21.0 and above no longer supports Python 2.7. The pip maintainers officially ended support for Python 2.7 in January 2021, coinciding with Python 2.7's end-of-life.

WARNING

Python 2.7 reached end-of-life on January 1, 2020. Using it presents security risks and compatibility issues.

Solutions for Different Environments

For CentOS/RHEL Systems

If you're using CentOS 7 or RHEL with yum package manager:

bash
# Remove the broken pip installation
yum remove python2-pip

# Reinstall the system pip
yum install python2-pip -y

# Upgrade to the last compatible version
pip install --upgrade "pip<21.0"

For Ubuntu/Debian Systems

On Ubuntu or Debian systems with apt:

bash
# Remove existing pip installation
sudo apt-get remove --purge python-pip
sudo apt-get autoremove

# Install the last compatible version
sudo easy_install pip==20.3.4

# Verify installation
pip --version

Using the Official Bootstrap Script

The recommended approach is to use the Python 2.7-specific bootstrap script:

bash
# Download the Python 2.7 compatible installer
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py

# Install pip
python get-pip.py

# Verify the installed version
pip --version  # Should show pip 20.3.4 or similar

Manual Cleanup and Reinstallation

If you have a corrupted installation that needs manual cleanup:

bash
# Remove pip files and directories
sudo rm -f /usr/local/bin/pip
sudo rm -f /usr/local/bin/pip2
sudo rm -rf ~/.cache/pip/
sudo rm -rf /usr/lib/python2.7/dist-packages/pip*

Then use the bootstrap script method above to reinstall.

Preventing Future Issues

To avoid accidentally upgrading to an incompatible pip version:

bash
# Always specify version constraint when upgrading
pip install --upgrade "pip<21.0"

# Or pin the version in requirements files
echo "pip<21.0" > requirements.txt

Although the original question asked for solutions without virtual environments, they remain the best practice for managing Python 2.7 environments:

bash
# Install virtualenv
pip install virtualenv

# Create a Python 2.7 virtual environment
virtualenv --python=python2 myenv

# Activate the environment
source myenv/bin/activate

# Install packages within the isolated environment
pip install your-package

DANGER

Python 2.7 is no longer maintained and contains unpatched security vulnerabilities. Migrate to Python 3 as soon as possible.

Checking Python Version Compatibility

For automation scripts or CI/CD pipelines, you can programmatically determine the appropriate pip version:

bash
use_pip_lt_21=$(python -c "import sys; print(sys.version_info[:2] < (3, 6))")
if [ "$use_pip_lt_21" = "True" ]; then
  pip_package="pip<21.0"
else
  pip_package="pip"
fi
pip install --upgrade "$pip_package"

Summary

The SyntaxError: invalid syntax when using pip with Python 2.7 occurs because pip versions 21.0+ use Python 3.6+ syntax features. The solution is to:

  1. Remove any existing broken pip installations
  2. Install pip version 20.3.4 (the last version supporting Python 2.7)
  3. Avoid upgrading beyond compatible versions
  4. Consider migrating to Python 3 for long-term compatibility

Always remember that while these solutions work, they are temporary fixes for an unsupported Python version that should be upgraded or replaced in production environments.