13

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

2
  • 1
    Maybe just create a symbolic link to /bin Commented Oct 8, 2016 at 2:25
  • Instead of #!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3 try #!/usr/bin/env python3 Commented Nov 7, 2019 at 17:23

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

5 Comments

$ sudo ln /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /usr/bin/python3 Password: ln: /usr/bin/python3: Operation not permitted
@WINSergey as you can read from MDr answer my solution is no longer working on MacOS >= El Capitan.
Yes @luca-angioloni, in my case work: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
@WINSergey I also updated my answer to address your problem.
Use: /usr/local/bin/python3
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.