How to (and should I) put a path to user-installed python ahead of system-installed python

bashhomebrewpathpythonterminal

I have a separate installation of Python 3.*, but the first python path in my $PATH variable is the path to system-installed Python. Is this how it should be?

I have added the path to user-installed Python using https://www.architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.Uydjga1dXDg, and it was popped at the end of $PATH.

Do I understand correctly that, in the current situation, when I run pip, brew etc. on python3 from bash, the changes will be applied to system-installed Python?

The fix I have got so far is to run export PATH=/usr/local/bin:/usr/local/sbin:$PATH every time I want to install or
update python, but it is not ideal.

NB: I went with pyenv solution as suggested by @bermudalocket. This pyenv tutorial was very useful.

enter image description here

Best Answer

You can accomplish this by adding

export PATH="/path/to/python:"$PATH

to your ~/.zshrc (or ~/.bash_profile if you're not on Catalina).

I'd like to propose an alternative and suggest pyenv, available via Homebrew. You can set a specific Python version to be "global" (i.e. default everywhere) and/or "local" (i.e. using that version in a specific directory only).

E.g.:

brew install pyenv
pyenv global 2.7.16
cd ~/myProjects/MyProject
pyenv local 3.7.6

If you were to use Python in ~/myProjects/MyProject it will default to 3.7.6, and anywhere else 2.7.16:

cd ~/some/other/directory
python --version
>> Python 2.7.16

cd ~/myProjects/MyProject
python --version
>> Python 3.7.6

https://github.com/pyenv/pyenv#homebrew-on-macos