121

What I'm trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3 with homebrew. This is the website that I'm following. http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx

I guess I followed every instruction well, got xcode freshly installed, Command line tools, and homebrew. But here's my little confusion occurs.

The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

I was really confused what this was, but I concluded that I should just add this following line at the bottom of ~/.profile file. So I opened the ~/.profile file by open .profile in the terminal, and added following line at the bottom. And now it looks like this.

export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Setting PATH for Python 3.6
# The original version is saved in .profile.pysave
export PATH=/usr/local/bin:/usr/local/sbin:$PATH

And then I did brew install python, and was hoping to see python3 when I do python --version. But it just shows me python 2.7.10. I want my default python to be python3 not 2.7

And I found a little clue from the website.

Do I have a Python 3 installed?

$ python --version
Python 3.6.4

If you still see 2.7 ensure in PATH /usr/local/bin/ takes pecedence over /usr/bin/

Maybe it has to do something with PATH? Could someone explain in simple English what PATH exactly is and how I could make my default python to be python3 when I run python --version in the terminal?

9
  • 6
    What's wrong with just running python3 (and pip3, etc.)? That's still the recommended solution for *nix systems at least until 2020. (If the extra character is too much for you, just alias py or py3 to python3, and it's even shorter than python.) Or, alternatively, have you considered using venv/virtualenv? Commented Apr 7, 2018 at 6:04
  • 1
    Meanwhile, if you want to understand what PATH is, you should not search Python-related sources for that, but general Unix resources. SuperUser or AskDifferent might be more relevant than StackOverflow, but really, you're asking someone to write a tutorial, there are already plenty of better tutorials online. Commented Apr 7, 2018 at 6:06
  • 1
    There are two different use cases here: yours and your system's. Let macos use the installed 2.7 version, otherwise you will have dependency issues. I would highly recommend installing second, dedicated distribution (such as Anaconda/conda, like virtualenv suggested above) if you want to program in python. Commented Apr 7, 2018 at 7:15
  • Adding things to the PATH twice just make things (a very little bit) slower. exporting PATH which is almost certainly already exported on your behalf by the system is also not useful. Commented Apr 7, 2018 at 7:28
  • I'm a beginner here in python so I just thought it should print python3. That's because what the website says so. If there's no problem with just typing python3, I will do that. Thanks for the explanation guys.Cheers :) Commented Apr 7, 2018 at 10:07

7 Answers 7

222

Probably the safest and easy way is to use brew and then just modify your PATH:

First update brew:

brew update

Next install python:

brew install python

That will install and symlink python3 to python, for more details do:

brew info python

Look for the Caveats:

==> Caveats
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

Then add to your path /usr/local/opt/python/libexec/bin:

export PATH=/usr/local/opt/python/libexec/bin:$PATH

The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python

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

8 Comments

I'm pretty sure Homebrew automatically updates the PATH. There's no reason to explicitly hardcode python libexec folder in there
I thought they stopped doing this last year: github.com/Homebrew/homebrew-core/pull/14408
Doing this and then updating pip (with pip3 install -U pip) causes subsequent runs of pip to error with pkg_resources.VersionConflict: (pip 19.1 (/usr/local/lib/python3.7/site-packages), Requirement.parse('pip==19.0.3')).
@philippos For mac add the path to /etc/paths to make it permanent stackoverflow.com/questions/22465332/… and this for Linux cyberciti.biz/faq/how-to-add-to-bash-path-permanently-on-linux
This answer still works for the the latest Homebrew formula that lists /usr/local/opt/[email protected]/libexec/bin in Caveats, since /usr/local/opt/python provides a suitable symlink.
|
70

Before we make the changes, the default version of python in my system was python 2.7.17.

python --version

Python 2.7.17

To make python3 as default python by replacing python2 in Ubuntu.

  1. Open Terminal
  2. cd
  3. nano ~/.bashrc
  4. alias python=python3 (Add this line on top of .bashrc file)
  5. Press ctr+o (To save the file)
  6. Press Enter
  7. Press ctr+x (To exit the file)
  8. source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)

python --version

Python 3.7.5

4 Comments

Answer is straight to the point. It worked on macOS catalina 10.15.3
Although the version is changed to python3, but the default python is still python 2.7. Like when you open a new jupyter notebook, or install a new package, the active python is still 2.7.
Very helpful answer in case ~/.bashrc not found. check for ~/.bash_profile or ~/.zshenv.
As @nightrain points out this will only work some of the time. You're not actually picking a new version of python system wide, this only alters the usage of python when invoked directly from the command line (and not from other programs which will not use your bash aliases)
39

Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create an alias.

If you are using zsh (the default on Mac OS) run the following from terminal:

echo 'alias python="python3"' >> ~/.zshrc
echo 'alias pip="pip3"' >> ~/.zshrc

If you're still using bash, echo into .bash_profile instead:

echo 'alias python="python3"' >> ~/.bash_profile
echo 'alias pip="pip3"' >> ~/.bash_profile

Note that the alias won't be defined until a new terminal session is started. To have the changes take affect within the same terminal window, you'll need to source your profile first:

source ~/.zshrc

or for bash:

source ~/.bash_profile

4 Comments

this only works for terminal sessions, however if you close your terminal and reopen your terminal you have source bashrc again. Is there a permanent solution?
@BigDreamz I don't understand what you mean by "permanent" ?
never mind, it works for me now. I had to source zshrc
works for macOS 12.6 when python is installed with brew, also had to do the same for pip3
5

According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on Python 2.

The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.

Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".

1 Comment

I decided that this is the best option, as you said: "python3". I just have to remember to type as well: "pip3" when installing new modules. Hehe.
0

I was facing the same issue with Homebrew, but had to do an extra manual step to fix the issue, because I got too new Python 3.11 instead of wanted Python 3.10

  • I installed awscli from Homebrew which pulled in Python 3.11 installation on my Homebrew

  • I had to reinstall Python 3.10

brew install [email protected]
brew link [email protected]

However even after this python3 command pulled the wrong version of Python. So as a missing manual fix, I had to do:

ln -s /opt/homebrew/bin/python3.10 /opt/homebrew/bin/python3

Then you can check if /opt/homebrew/bin gets preference (is higher) when you check PATH configured in the .rc file of your shell:

echo $PATH | tr ":" "\n"
/opt/homebrew/bin
/Users/moo/.local/bin
/Users/moo/go/bin
/Users/moo/.go/bin
/Users/moo/.nvm/versions/node/v16.15.0/bin
/Users/moo/.zsh/bin
...

Comments

0

I struggled tackling this, because I installed python version 3.11 using pip. But on running python3, I was still getting 3.8. To fix this problem I did the following steps:

1: Opened ~/.bash_profile and looked for python upon searching I found two paths. One was corresponding to version 2.x. Whereas other was referring to version 3.8.

# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

# Setting PATH for Python 3.8
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH

2: I commented 3.8 line and replaced it the 3.11 brew python, which for me was /usr/local/opt/[email protected]/bin.

So the final output looks like below:

# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

# Setting PATH for Python 3.8
# The original version is saved in .bash_profile.pysave
# PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
PATH="/usr/local/opt/[email protected]/bin:${PATH}"
export PATH

3: After this step, I just closed the terminal opened it again and upon typing python3 --version, I got the correct output, which was Python 3.11.5.

Comments

0

Assuming you have used brew to install Python, you can use the brew --prefix command to manipulate your PATH so that it works on both Intel as well as Apple chip based Macs:

brew_python=$(brew --prefix [email protected])
PATH="$brew_python/bin/python3.9:$brew_python/libexec/bin:${PATH}"

Add the above code to your $HOME/.bash_profile (if you are a bash user) or $HOME/.zprofile if you use zsh.

  • $brew_python/bin/python3.9 ensures all python3 commands are made available to you

  • $brew_python/libexec/bin ensures that aliases like python, pip, pydoc, and wheel are made available to you, without the need for any aliases or hard links

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.