'Centos - No module named yum

Environment: Aws EC2, redhat - centos

While installing awscli, due to incompatibility between awscli2 and python2.6 , I had to install python 3.7. But while doing this i removed all existing python packages including default system usage onces.

Now when I try to execute yum, it gives following error:

/usr/bin/python: bad interpreter: No such file or directory

Then, I installed python2.6 (and also tried 2.7) manually with following way.

sudo wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz 

sudo tar xzf Python-2.7.9.tgz

cd Python-2.7.9

sudo ./configure --enable-optimizations

sudo make & sudo make install

But again it gives same error:

When i change default python directory

sudo vim `which yum`

/usr/local/bin/python2.7 and tried also /usr/local/bin/python2.6

Also for 2.7 I tried adding following

sys.path.append('/usr/local/bin/python2.7/site-packages')
sys.path.append('/usr/lib64/python2.7/site-packages')

But now it gives following error.

No module named yum



Solution 1:[1]

I'm not sure if this is a valid answer, but I've put a few hours into this and the conclusion is to reinstall the system.

Other way may possibly be a thorough investigation of all package dependencies, downloading the rpms manually and updating using rpm command, but that is another few hours. Since we are going to reinstall the whole system pretty soon, it's not a big concern.

We will use Debian 11, not CentOS. CentOS 7 is really old.

It needs to be noted that this problem would not occur, if the new Python would be "altinstall"ed instead of "install"ed in the first place.

Solution 2:[2]

Well, you know which python it needs (the path) - presumably python 2.6 on your original system:

/usr/bin/python: bad interpreter: No such file or directory

And you know where python gets installed into, when built from scratch:

/usr/local/bin/python2.6

In general it's not a good idea to remove your system's python installation. This is why there are tools like pyenv for instance.

Back to your problem - You could create an alias for the time being, either temporarily in your shell or persistently in your ~/.bashrc (assuming you use bash):

alias python='/usr/local/bin/python2.6'

This will, for your user and your session, make python link to /usr/local/bin/python2.6.


By the way, problems like this:

No module named yum result from the fact that dependencies are installed for specific python versions only, i.e.:

# This
python2.6 -m pip install requests
# installs 'requests' for python2.6 only.
# That means for python2.7, there is no 'requests' module.

I can only recommend pyenv to manage multiple python versions in your system and a python dependency management tool like e.g. poetry to define dependencies and install them correctly in terms of python version, constraints and dependencies of dependencies.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tomas Tudja
Solution 2