476

I'm running Mountain Lion and the basic default Python version is 2.7. I downloaded Python 3.3 and want to set it as default.

Currently:

$ python
    version 2.7.5
$ python3.3
    version 3.3

How do I set it so that every time I run $ python it opens 3.3?

2

21 Answers 21

657

Changing the default python executable's version system-wide could break some applications that depend on python2.

However, you can alias the commands in most shells, Since the default shells in macOS (bash in 10.14 and below; zsh in 10.15) share a similar syntax. You could put alias python='python3' in your ~/.profile, and then source ~/.profile in your ~/.bash_profile and/or your~/.zsh_profile with a line like:

[ -e ~/.profile ] && . ~/.profile

This way, your alias will work across shells.

With this, python command now invokes python3. If you want to invoke the "original" python (that refers to python2) on occasion, you can use command python, which will leaving the alias untouched, and works in all shells.

If you launch interpreters more often (I do), you can always create more aliases to add as well, i.e.:

alias 2='python2'
alias 3='python3'

Tip: For scripts, instead of using a shebang like:

#!/usr/bin/env python

use:

#!/usr/bin/env python3

This way, the system will use python3 for running python executables.

16
  • 3
    Should this not be put in ~/.bash_profile instead of ~/.bash_aliases? Jul 2, 2016 at 23:30
  • 4
    Putting alias python=python3 and then running python in my terminal on osx el capitan didn't work for me. Tried saving it both ~/.bash_aliases and ~/.bash_profile.
    – Haymaker87
    Jul 26, 2016 at 15:41
  • 51
    @Haymaker87 run source ~/.bash_profile after edit ~/.bash_profile file.
    – Wei Lu
    Aug 1, 2016 at 22:08
  • 17
    You can do the same for pip: alias pip='pip3.6'
    – tread
    Aug 12, 2017 at 9:01
  • 1
    @SantoshKumar If you're using virtualenv/virtualenvwrapper for activation do you still need to use #!/usr/bin/env python3 or any other shebang + path at the top of python files?
    – Edison
    Oct 8, 2017 at 23:05
221

You can solve it by symbolic link.

unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python
15
  • 38
    This is the correct answer (aliases are nice but only accessible by bash, which limits where you can call from). However, I would use unlink instead of rm to remove symlinks (if you accidentally add a trailing slash on rm you might have some bad results). Alternatively, you could do ln -s -f ... which should overwrite the current symlink.
    – Chad Befus
    Oct 27, 2017 at 15:37
  • 3
    Does it have any consequences for scripts that expect python to be python2.7? Nov 21, 2017 at 23:30
  • 32
    macOS: unlink: /usr/bin/python: Operation not permitted
    – MarksCode
    Jun 23, 2018 at 17:30
  • 5
    @MarksCode try to just run the second original line (i.e. ln -s... with /local/).
    – EliadL
    Sep 25, 2018 at 16:19
  • 4
    For Mac OS users, you should use the brew commands to make the symlinks automatically. For example: brew unlink python, brew switch python 3.x.x_x (or 2.x.x), brew python link python (or python@2). Note, brew installs python 3 as "python3".
    – jjwdesign
    Jul 17, 2019 at 17:50
143

Open ~/.bash_profile file.

vi ~/.bash_profile

Then put the alias as follows:

alias python='python3'

Now save the file and then run the ~/.bash_profile file.

source ~/.bash_profile

Congratulation !!! Now, you can use python3 by typing python.

python --version

Python 3.7.3

5
  • 2
    If you found like I did that the same issue applies for pip (i.e. the pip --version is still 2.7.x) then you'll need to do the same steps for pip3. vi ~/.bash_profile, alias pip='pip3', source ~/.bash_profile, pip --version Jan 13, 2020 at 21:37
  • 6
    Do I really need to source ~/.bash_profile everytime I re-opened my terminal? It reverts to python2 once I close and open the terminal again. Apr 21, 2020 at 2:46
  • 1
    @RickyAguilar Me either. My current conda base is 3.8, I want to change the default pythton to 3.10. I have changed the ~/.bash_profile, by commenting out the existing one and adding export PATH="/Applications/anaconda3/envs/py3.10/bin:$PATH" it works after source. However, once I quite the terminal and reopen, it turns out the base is still py3.8. and i need to source again. Which is unresonable. Since I can conda activate py3.10. Have u find another way to tackle this?
    – CN_Cabbage
    Apr 9, 2022 at 5:17
  • if you already installed visual studio code it will be easy to use. code ~/.bash_profile instead of using. vi ~/.bash_profile.
    – zpvk
    Jun 30, 2022 at 10:39
  • Just this code worked for my Mac, thanks
    – Rai Rz
    Jul 6, 2022 at 16:37
131

I encountered this issue as well, so I thought I should post an updated answer. Please note that this will only apply to a Mac-based setup (I haven't tried it with Windows or any flavor of Linux). The simplest way to get this working is to install Python via Brew. If you don't have brew installed, you will need to do that first. Once installed, do the following in at the terminal:

brew install python

This will install Python 3. After it's installed, run this:

ls -l /usr/local/bin/python*

You will see all of the links created by brew to its Python install. It will look something like this:

lrwxr-xr-x  1 username  admin  36 Oct  1 13:35 /usr/local/bin/python3@ -> ../Cellar/python/3.7.4_1/bin/python3
lrwxr-xr-x  1 username  admin  43 Oct  1 13:35 /usr/local/bin/python3-config@ -> ../Cellar/python/3.7.4_1/bin/python3-config
lrwxr-xr-x  1 username  admin  38 Oct  1 13:35 /usr/local/bin/python3.7@ -> ../Cellar/python/3.7.4_1/bin/python3.7
lrwxr-xr-x  1 username  admin  45 Oct  1 13:35 /usr/local/bin/python3.7-config@ -> ../Cellar/python/3.7.4_1/bin/python3.7-config
lrwxr-xr-x  1 username  admin  39 Oct  1 13:35 /usr/local/bin/python3.7m@ -> ../Cellar/python/3.7.4_1/bin/python3.7m
lrwxr-xr-x  1 username  admin  46 Oct  1 13:35 /usr/local/bin/python3.7m-config@ -> ../Cellar/python/3.7.4_1/bin/python3.7m-config

The first row in this example shows the python3 symlink. To set it as the default python symlink run the following:

ln -s -f /usr/local/bin/python3 /usr/local/bin/python

Similarly you can set the pip symlink too

ln -s -f /usr/local/bin/pip3 /usr/local/bin/pip

You will have to reload your current terminal shell to use the new symlink in that shell. Run this command to reload your shell:

exec $SHELL -l

You're all set now. Now, you can do:

which python

and it should show:

/usr/local/bin/python

All newly opened shell sessions will (should) automatically use the new symlink. To test this, open a new terminal shell and run the following:

python --version
8
  • 5
    Works great (Reference)
    – Nepo Znat
    May 23, 2020 at 8:38
  • 2
    "You will have to reload your current terminal shell for it to use the new symlink in that shell." Thanks, that was the piece I was missing from all the answers so far.
    – cs_pupil
    Jan 21, 2021 at 20:30
  • 2
    this also worked for me. A few things I would like to add - 1. I am using Mac M1, so I had to run arch -arm64 brew install python & 2. pip wasn't working for me, so I had to run sudo ln -s -f /usr/local/bin/pip3 /usr/local/bin/pip and then pip worked. Feb 3, 2022 at 14:56
  • @sohammondal Thanks you. I appreciate your input.
    – sknight
    Feb 3, 2022 at 17:52
  • 1
    I tried many solutions and this was the only one that worked - thank you so much!!! Nov 21, 2022 at 15:31
36

Go to terminal type:

alias python=python3.x

This will setup default python as python3.x

2
  • 6
    This worked for me but using "alias python=python3"
    – Discant
    Mar 15, 2020 at 21:03
  • 6
    This will temporarily set python version for that terminal instance.
    – Ashwin R
    Apr 3, 2020 at 6:46
21

This worked for me. I added alias and restarted my terminal:

alias python=/usr/local/bin/python3
0
17

The following worked for me

cd /usr/local/bin
mv python python.old
ln -s python3 python
0
12

Go to 'Applications', enter 'Python' folder, there should be a bash script called 'Update Shell Profile.command' or similar. Run that script and it should do it.

Update: It looks like you should not update it: how to change default python version?

11

I believe most of people landed here are using ZSH thorugh iterm or whatever, and that brings you to this answer.

You have to add/modify your commands in ~/.zshrc instead.

10
$ sudo ln -s -f $(which python3) $(which python)

done.

4
  • 2
    ln: /usr/bin/python: Operation not permitted
    – Mustafa
    Feb 18, 2022 at 1:12
  • ln: found: No such file or directory Sep 21, 2022 at 9:31
  • usage: ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file [target_file] ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file ... target_dir link source_file target_file
    – dy_
    Jan 27 at 2:10
  • @dy_ your comment is unclear. just try: sudo ln -s -f $(which python3) /usr/local/bin/python
    – oori
    Jan 28 at 22:35
9

Mac users just need to run the following code on terminal

brew switch python 3.X.X

3.x.x should be the new python version.

This will update all the system links.

UPDATE

For Newer version of MAC use

brew link python 3.X.X
6
  • Not sure why you got a down vote. This worked for me! Thanks for posing this. I upvoted you to cancel that down vote ;) Oct 16, 2020 at 16:55
  • 2
    On Catalina (10.15.17), I get: Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.
    – cs_pupil
    Jan 21, 2021 at 20:27
  • 7
    this does not work for me
    – MIike Eps
    Jul 18, 2021 at 15:44
  • Error: Unknown command: switch
    – Xitcod13
    Dec 3, 2021 at 23:05
  • 1
    For me it looked like this: brew link python@3.9. Thanks for hints! Jun 28, 2022 at 7:31
7

Suggestions to alias python to python3 will cause problems with virtual environments that set the version of python (eg: pyenv). With pyenv, you can set the version globally like so:

pyenv global 3.8.2

and then in any specific project, you can create a .python-version file which has the python version inside of it:

pyenv local 2.7.1

This is the best way to manage multiple versions of python on a system in my opinion.

4

On MacOS

Step-1: Upgrade python to latest version by: $ brew upgrade python

Step-2: Go to home: $ cd

Step-3: open .bash_profile

$ vi .bash_profile

Setting PATH for Python 3.8

PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}" export PATH

Step-4: Save the file. And compile it by:

$ . .bash_profile

Step-5: Check the python version:

$ python -V

Step-6: Thats all.

3

I think when you install python it puts export path statements into your ~/.bash_profile file. So if you do not intend to use Python 2 anymore you can just remove that statement from there. Alias as stated above is also a great way to do it.

Here is how to remove the reference from ~/.bash_profile - vim ./.bash_profile - remove the reference (AKA something like: export PATH="/Users/bla/anaconda:$PATH") - save and exit - source ./.bash_profile to save the changes

1
  • It does not happen
    – 0x12
    Dec 4, 2017 at 2:56
3

This is the simplest way from my exp. (if you have brew installed on your mac).

Try this from your terminal:

brew install python3

and then run the below on your terminal :

ls -l /usr/local/bin/python*

Tip:

** (note down the python version 3.8 or 3.9 thats displayed on the terminal. This will be required in the next step). for e.g. in my case it was:

lrwxr-xr-x 1 user admin 24 May 7 14:33 /usr/local/bin/python -> /usr/local/bin/python3.9

Now run the below command on your terminal:

ln -s -f /usr/local/bin/python3.9 /usr/local/bin/python

(where 3.9 is the version displayed on your terminal with the previous command)

Its DONE !

To test your default version of python:

  1. close the current terminal or start a new terminal and
  2. run the below command :

python --version

Happy Coding!

2

For me the solution was using PyCharm and setting the default python version to the the one that i need to work with.

install PyCharm and go to file ==> preferences for new project, then choose the interpreter you want for your projects, in this case python 3.3

2

If you use macports, you do not need to play with aliases or environment variables, just use the method macports already offers, explained by this Q&A:

How to: Macports select python

TL;DR:

sudo port select --set python python27
1

I'm not sure if this is available on OS X, but on linux I would make use of the module command. See here.

Set up the modulefile correctly, then add something like this to your rc file (e.g. ~/.bashrc):

module load python3.3

This will make it so that your paths get switched around as required when you log in without impacting any system defaults.

1

If you are using a virtualenvwrapper, you can just locate it using which virtualenvwrapper.sh, then open it using vim or any other editor then change the following

# Locate the global Python where virtualenvwrapper is installed.
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
    VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi

Change the line VIRTUALENVWRAPPER_PYTHON="$(command \which python)" to VIRTUALENVWRAPPER_PYTHON="$(command \which python3)".

1

If you are using macports, that has a easier way to do:

run:

port install python37

after install, set default:

sudo port select --set python python37

sudo port select --set python3 python37

restart your cmd window, finished.

1
  • This was the only solution that worked for me on M1 Mac
    – Kousha
    Aug 31, 2022 at 19:18
1

Well... It's kinda old. But still deserves a good answer.

And the good one is You Don't Wanna Touch The Default Python On Mac.

Install any Python version you need via Homebrew or whatever and use it in virtualenv. Virtualenv is often considered to be something crap-like, but it's still way, wayyyy better than changing python version system-wide (macOS is likely to protect itself from such actions) or user-wide, bash-wide... whatever. Just forget about the default Python. Using playgrounds like venv is what your OS will be most, very most grateful for.

The case is, for example, many modern Linux distributions get rid of Python2 installed out-of-the-box, leaving only Python3 in the system. But everytime you try to install something old with python2 as a dependency... hope you understand what I mean. A good developer doesn't care. Good developers create clean playgrounds with python version they desire.

Not the answer you're looking for? Browse other questions tagged or ask your own question.