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?
Advertisement
Answer
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.
Update
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.