Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install Python 3 to /usr/bin/ 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

like image 745
boated_tw Avatar asked Oct 08 '16 02:10

boated_tw


People also ask

How do I add python 3 to my Mac?

Install Python 3 with the Official Installer First, download an installer package from the Python website. To do that, visit https://www.python.org/downloads/ on your Mac; it detects your operating system automatically and shows a big button for downloading the latest version of Python installer on your Mac.

How do I install python usr bin?

Install your “dev” Python into /usr/local/python-x.y.z or a similar location. Setup your shell (e.g. path variable and aliases) so that “python” and “python3” will run your preferred version. In scripts, use #!/usr/bin/python3 to run with system Python. Use #!/usr/bin/env python3 to run with your dev version.


2 Answers

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

like image 198
MDr Avatar answered Oct 13 '22 20:10

MDr


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.

like image 28
Luca Angioloni Avatar answered Oct 13 '22 19:10

Luca Angioloni