How to Recover Deleted Python and Fix Yum in CentOS 7 Without Reinstalling OS


2 views

When Python gets accidentally removed from critical system paths in CentOS 7, it breaks fundamental system tools like yum that rely on Python 2.7. The error -bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory occurs because yum's shebang line points to the now-missing Python interpreter.

First, we need to temporarily restore Python functionality to repair the system. Here's how to bootstrap Python back onto the system:

# Download Python 2.7 RPM package directly
wget http://vault.centos.org/7.9.2009/os/x86_64/Packages/python-2.7.5-89.el7.x86_64.rpm

# Force install without dependencies (we'll fix them later)
rpm -ivh --nodeps python-2.7.5-89.el7.x86_64.rpm

With Python minimally restored, we can now repair yum and its dependencies:

# Reinstall critical Python packages
yum reinstall -y python python-libs python-devel python-setuptools

# Verify Python paths are restored
ls -la /usr/bin/python*
which python

After restoring core Python functionality, perform a full system verification:

# Reinstall all Python-related packages
yum reinstall -y @development python-* yum-*

# Verify package integrity
rpm -Va python-* | grep -v '^..5'

# Test yum functionality
yum check-update

For more severe cases where RPM itself is broken, try these methods:

# Method 1: Use rpm2cpio to extract Python from another system
rpm2cpio python-2.7.5-89.el7.x86_64.rpm | cpio -idmv

# Method 2: Manual Python binary placement
scp root@healthy-server:/usr/bin/python2.7 /usr/bin/
chmod +x /usr/bin/python2.7
ln -s /usr/bin/python2.7 /usr/bin/python

To avoid similar situations:

  • Always use yum remove instead of manual deletion
  • Consider using virtual environments for development
  • Maintain backup copies of critical binaries
# Example cron job for binary backups
0 3 * * * tar czf /backup/system_binaries_$(date +\%Y\%m\%d).tar.gz /usr/bin/python* /usr/lib/python*



When Python gets accidentally removed from CentOS 7, it creates a chain reaction of system failures because:

  • YUM is written in Python (uses /usr/bin/python as interpreter)
  • Core system utilities depend on Python 2.7
  • Package management becomes impossible

Method 1: Manual Python Reinstallation

# First check rpm database for remaining Python packages
rpm -qa | grep -i python

# Download Python 2.7 RPMs manually (adjust versions as needed)
wget http://vault.centos.org/7.9.2009/os/x86_64/Packages/python-2.7.5-89.el7.x86_64.rpm
wget http://vault.centos.org/7.9.2009/os/x86_64/Packages/python-libs-2.7.5-89.el7.x86_64.rpm

# Force reinstall with rpm (ignore dependencies temporarily)
rpm -ivh --replacefiles --replacepkgs python-*.rpm

Method 2: Using rpm2cpio (When YUM is completely broken)

# Extract Python from CentOS ISO/DVD
mkdir /tmp/pythonfix
cd /tmp/pythonfix
rpm2cpio python-2.7.5-89.el7.x86_64.rpm | cpio -idmv
rsync -av usr/ /usr/
# Verify Python binary exists
ls -la /usr/bin/python

# Test basic functionality
python -c "import sys; print(sys.version)"

# Fix yum's python interpreter path if needed
sed -i 's|/usr/bin/python|/usr/bin/python2.7|' /usr/bin/yum
  • Always use yum remove instead of manual deletion
  • Consider Python virtual environments for development
  • Maintain backup RPMs for critical packages

For remote servers without recovery options:

# Contact hosting provider for:
1. Emergency console access
2. Filesystem restore from backup
3. Reinstallation from rescue mode

# Cloud providers often offer:
- Snapshot rollback features
- Rebuild-from-image options
- Automated backup restoration