I have installed Python 2.7.9 in /usr/local/bin
. Now it doesn't work any more. I have another Python in /usr/bin/
but in the path is /usr/local/bin/
first. How can i remove the 2.7.9 Python?
Your question is lacking in details, the most pertinent being how you actually installed Python into /usr/local/bin
. The installation method would indicate how to remove the installed files.
The most common way of installing packages into the /usr/local
hierarchy of directories to is to compile from source and to run sudo make install
after compiling and linking. If you didn't already remove the original (uncompressed) source directory, you can change into it and remove the compiled Python package by running:
sudo make uninstall
If the source code has been deleted, you could try re-downloading the source again.
If there’s no uninstall
target for make
(unfortunately, more common than you might think), another (inelegant) option is to use the find
command to search for all files in /usr/local
directory tree that have the same modification time as other files in the application that you want to remove.
These days, I would recommend installing the checkinstall
tool. Instead of running make install
, this can be used to create an RPM or Debian package which can then be installed (and uninstalled) using the system’s regular software installation tools.
DISCLAIMER: I've since learned a lot, and would recommend setting environment variables for a shell or shell session rather than use this answer. For example, if you manually relink the system's Python2 interpreter to a Python3 interpreter, you may wreak havoc on your system. Please use this answer with caution.
Just reset the symlink.
First, find out which python:
$ which python
In my case, I get:
/usr/local/bin/python
Then find where the symlink points to
$ file /usr/local/bin/python
/usr/local/bin/python: symbolic link to `/usr/bin/python'
Then just repoint the symlink back to the default (in this case, I use the default: /usr/bin/python).
No uninstalls necessary.
I've since found a lot of better ways to enact this exact same behavior, without having effects on the entire system.
Say I have an undesired python
install in /usr/bin
, and a desired python
install in /opt/bin
. Let's say for the point of comparison that the /usr/bin
is Python 3.5, and the /opt/bin
is Python 2.7. This would create immediate consequences for using the wrong Python interpreter, rather than subtle errors down the line.
Application Defaults
If you would like to (on Linux systems) change which interpeter runs Python scripts, you can change this either via a GUI, or via xdg-mime (a walkthrough can be found here). For macOS or Windows, this can be done easily through a GUI.
Interactive Shell
If you would like to change the default Python for a specific shell, I can see two good ways of doing this. One would be to change the default search PATH
to set /opt/bin
before usr/bin
for a specific situation, however, if you have numerous alternative installs to system packages, this might pose issues too. Another would be to set an alias for Python to the version you want to use. This is the preferred solution, as it only changes the interpreter and is merely a shortcut to reference an existing command.
For example, to set the alias I could use:
alias python="/opt/bin/python"
And to change the default path, I could use:
export PATH=/opt/bin:$PATH
Adding these lines to ~/.bashrc
or ~/.bash_aliases
(the latter is Ubuntu-only by default) will make these shortcuts be the default on any interactive shell you start. Combining application defaults and interactive shell scripting allows you to have tight control over which interpreter runs your code, but does not require interfering with potentially crucial system files.
Your PATH environment variable. It has a list of directories which bash searches (in the same order) when it's looking for an program to execute. Basically you want to put /usr/local/bin at the start of your PATH environment variable. Add the following to your ~/.bashrc file:
export PATH=/usr/local/bin:$PATH
You can have a look at the current setting by running the set command in bash.
Alternatively, you can simply rename /usr/bin/python to /usr/bin/python2.3 and create a symlink pointing to the new version, e.g.
ln -s /usr/local/bin/python /usr/bin/python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With