Install Python 3 to /usr/bin/ on macOS

MacOS

Question or issue on macOS:

I installed python2.x and python3.x using homebrew and the executable python paths are listed below:

$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

$ which python3
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

It’s quite too long and not so clean to write a shebang in a python code to make it runnable on Terminal:

#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python OR
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

I prefer

#!/usr/bin/python OR
#!/usr/bin/python3

My issue here is, how can I correcly move or reinstall python on macOS to /usr/bin such as /usr/bin/python OR /usr/bin/python3 Instead of
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

How to solve this problem?

Solution no. 1:

This is NOT possible on Mac OS X El Capitan anymore as from then on System Integrity Protection prevents that. More info in Cannot create a symlink inside of /usr/bin even as sudo

Solution no. 2:

Create a symbolic link in /usr/bin/

Open terminal and do:

$ sudo ln /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/bin/python
$ sudo ln /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 /usr/bin/python3

You can now do what you wanted to do.

Edit: Unfortunately as you can read from the other answers, this solution no longer works on MacOS >= El Capitan due to System Integrity Protection. (See here)

A possible alternative is to use the folder /usr/local/bin that should be accessible.

Solution no. 3:

The basic question in the OP seems to not be doable because the newer Mac OSes have “System Integrity Protection” which prevents “unauthorized” changes to key directories such as /usr/bin, and sudo cannot override that.

The suggestion of using /usr/local/bin and /usr/local/Frameworks seems like it should work (I haven’t tried it). However, at https://opensource.com/article/19/5/python-3-default-mac, Matthew Broberg suggests that it is likely to create problems when updating.

In that same article, Moshe Zadka recommends using pyenv to manage Python environments and using shell aliases rather than symlinks.

I was going to go with the /usr/local suggestion above, but having found this contraindication, I’m going to try Moshe’s method. I’ll report back here if I hit any snags.

Update: I followed the method and I still was getting pip not found and which python and python -V were not giving me the expected results. Doh! I ran:

. ~/.bash_profile

(where I had put the recommended eval "$(pyenv init -)" command) and suddenly everything was finding the paths and versions as expected.

Hope this helps!