Changing your default Python install: one tiny wrinkle that shouldn’t have tripped me up
Categories
So you’ve installed a later and greater version of Python (Python 3.6.1 in my case) in /usr/local/bin and would like it to be used instead of your server’s default (and presumably older) python version when you type python at the shell prompt after logging in? The solution is widely documented.
- Add the following to the end of your
.bash_profile, just before theexport PATHline:PATH=/usr/local/bin:$PATH
- When you reload
.bashrcor logout and login again, youll typepythonat the shell prompt and feel a twinge of annoyance at seeing you’ve been given the old Python install. At this point, if you’re (as clueless as) me, you’ll check to make sure that the the shell you’re using is indeedbashby typingecho $SHELLat the prompt. That returned/bin/bashin my case. If you are a bit wiser than I am, you’ll have first echoed the$PATHand seen that, yes,/usr/local/binis being prepended. - I’m sure you won’t do what I did, which was flail a bit, editing my
.bash_profileand.bashrcfiles repeatedly and, somewhere along the line, introducing an error that led to many duplicate directory paths being added to$PATH, requiring me to find and run a code snippet to clean it up. The third solution from akostadinov’s answer to Remove duplicate $PATH entries with awk command on unix.stackexchange.com did the trick for me:PATH=`printf %s "$PATH" | awk -v RS=: '{ if (!arr[$0]++) {printf("%s%s",!ln++?"":":",$0)}}'` - Here’s what you’ll probably do immediately after noticing that you aren’t getting your custom Python install. Realizing that bash is looking where you’ve told it to look but not finding anything named
Python, you’llcdinto/usr/local/binand have a look around. The Python executable in/usr/local/binis namedPython[MajorVersion].[MinorVersion](Python3.6for me). There may be a symlink to it namedPython[MajorVersion](e.g.Python3) but to make the commandpythoncall your custom Python, you’ll need to create another symlink:ln -s python3.6 python
Shazaam! Fixed.