It took a while to run down why certain older versions of programs were always launched when I was in Visual Studio Code, but not when I opened a terminal window. I first found this in relationship to nvm and documented it in this post:
VS Code and the Node Version Manager. Unfortunately it affects other commands too, and it isn't just a VS Code issue, so I thought a more general post was in order.
Of course we know that the PATH environment variable is a colon-separated list of directories that are searched sequentially and the first match for a command is used:
$ echo $PATH
/usr/local/Cellar/python/3.7.4/bin:/usr/bin:/usr/local/bin
So in ~/.bash_profile we make sure that we set up the path in the correct order. For example, there is a python program in /usr/bin, so if we install a new python version we make sure the PATH points to the new installation before it points to /usr/bin. Python 3.7 has to come before /usr/bin in the PATH!
Some folks like to put PATH modifications in ~/.bashrc which is fine. You just have to remember that ~/.bashrc is supposed to run for every bash instance, so don't keep extending the PATH every time it gets called. This post explains it:
Always Modify PATH and Other Variables Conditionally.
The problem is that MacOS VS Code (and other tools) may launch bash with a -l or --login option. This creates a problem on MacOS because the bash program has been altered so that -l forces /usr/bin and /usr/local/bin to the front of the PATH. It is actually a really good security feature to make sure that built-in programs are located before a Trojan horse. Unfortunately sometimes we need to override that. Specifically, three important things that happen when -l is used: