480

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?

U3.1415926
  • 812
  • 12
  • 30
Marcus
  • 9,032
  • 11
  • 45
  • 84

21 Answers21

661

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.

rustyMagnet
  • 3,479
  • 1
  • 31
  • 41
Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
  • 3
    Should this not be put in ~/.bash_profile instead of ~/.bash_aliases? – UnsettlingTrend Jul 02 '16 at 23:30
  • 5
    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 '16 at 15:41
  • 52
    @Haymaker87 run `source ~/.bash_profile` after edit `~/.bash_profile` file. – Wei Lu Aug 01 '16 at 22:08
  • 17
    You can do the same for pip: `alias pip='pip3.6'` – tread Aug 12 '17 at 09:01
  • @surfer190 that will cause problem when running with sudo, isn't it? – Santosh Kumar Aug 12 '17 at 09:36
  • 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 08 '17 at 23:05
  • Is this everything that is required for me to switch to python3 in my shell and applications? – Alper Oct 20 '17 at 07:46
  • @Alper this was for shell only. The tip part is for developers, but don't change if either one of them is already there. It might break. – Santosh Kumar Oct 20 '17 at 07:55
  • Shouldn't cause any issues with sudo but will cause issues if you don't source your alias. This is more of a workaround. – Henry Tseng Jul 12 '18 at 17:55
  • It won't take until you close your terminal window after the change to the .bash_profile are made. – Taylor A. Leach Nov 27 '18 at 03:28
  • you should set the pip alias not to pip3.6 specifically, instead set it to pip3: ``alias pip='pip3' – AlexeyGy Jun 07 '19 at 11:42
  • adding -> alias python='python3' in ~/.bash_profile file worked for me. – Dev1ce Jul 15 '19 at 03:26
  • Would it still break things now in Catalina? –  Oct 07 '19 at 23:42
  • Nope, its *bash* thing. Nothing to do with the operating system. – Santosh Kumar Oct 08 '19 at 01:56
  • Do I need to `source ~/.bash_profile` everytime i close and open my terminal? It says command not found after I re-opened my terminal – Ricky Aguilar Apr 21 '20 at 02:37
  • Whilst this is a quick fix there are some potential issues with this solution, especially because you still depend on the python version Apple ships by default. [This article](https://opensource.com/article/19/5/python-3-default-mac) explains them in depth as well as alternative solutions that avoid these problems. – Robin Nov 12 '20 at 10:34
227

You can solve it by symbolic link.

unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python
Almog Cohen
  • 1,283
  • 1
  • 13
  • 12
Shin Kim
  • 4,911
  • 3
  • 29
  • 31
  • 39
    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 '17 at 15:37
  • 2
    @ChadBefus Thank you for your reply. I agree with your opinion. unlink is safer than rm. – Shin Kim Nov 15 '17 at 12:20
  • 3
    Does it have any consequences for scripts that expect `python` to be `python2.7`? – Anton Tarasenko Nov 21 '17 at 23:30
  • 2
    Can you do the reverse to reestablish python2x as the default version? So if you have linked python with python3, I'm suggesting then you can unlink python3 and ln -s python2. – Jazzmine Dec 06 '17 at 10:57
  • 1
    it does nothing – user924 May 23 '18 at 07:00
  • 32
    macOS: `unlink: /usr/bin/python: Operation not permitted` – MarksCode Jun 23 '18 at 17:30
  • 5
    @MarksCode try to just run the second original line (i.e. `ln -s...` *with* `/local/`). – EliadL Sep 25 '18 at 16:19
  • If your app requires python3 for running, you should edit symlinks not alias. – kidkkr Mar 01 '19 at 07:05
  • 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 '19 at 17:50
  • This did not work for me. After following this still python2 is invoked by default. – Ganesh M S Sep 26 '19 at 00:22
  • Doesn't this risks the application dependent on python2? Definitely it will go away when Python 3 is mainstream, but still. Correct me if I'm missing something. – Santosh Kumar Dec 12 '19 at 20:13
  • 3
    The *real* major problem with recent Mac OSs is it doesn't give a $#%^ about UNIX commands and notion of root: `>> sudo unlink /usr/bin/python` `Password:` `unlink: /usr/bin/python: Operation not permitted` `>> sudo -su root` `>> whoami` `root` `>> unlink /usr/bin/python` `unlink: /usr/bin/python: Operation not permitted` – Vlad K. Jan 06 '20 at 01:43
  • 1
    This feels like the cleaner approach. I set mine to python 3.5 and it worked. – TimewiseGamgee Aug 03 '20 at 01:50
  • 2
    I had to close terminal and open a new one to get it working in macOS – Pei Oct 18 '20 at 18:47
  • I had to do this in my case ln -s -f /usr/local/Cellar/python@3.8/3.8.11/bin /usr/local/bin/python3 – Nikhil Das Nomula Jul 19 '21 at 16:19
147

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

Ananta Chandra Das
  • 1,865
  • 2
  • 14
  • 18
  • 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 – SnellyBigoda Jan 13 '20 at 21:37
  • 7
    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. – Ricky Aguilar Apr 21 '20 at 02: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 09 '22 at 05: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 '22 at 10:39
  • Just this code worked for my Mac, thanks – Rai Rz Jul 06 '22 at 16:37
135

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
Viraj Singh
  • 1,951
  • 1
  • 17
  • 27
sknight
  • 2,009
  • 1
  • 14
  • 15
  • 5
    Works great ([Reference](https://dev.to/malwarebo/how-to-set-python3-as-a-default-python-version-on-mac-4jjf)) – Nepo Znat May 23 '20 at 08: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 '21 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. – sohammondal Feb 03 '22 at 14:56
  • @sohammondal Thanks you. I appreciate your input. – sknight Feb 03 '22 at 17:52
  • Works great on Intel iMac Pro (2017). It fixed the issue I was having of not being able to update following the other answers above. – Olney1 Aug 06 '22 at 19:24
  • Following my comment yesterday I have discovered that, after I quit the terminal, the changes do not stick. How do you make the changes permanent? – Olney1 Aug 07 '22 at 07:27
  • but I'm getting. 'zsh: no matches found: /usr/local/bin/python*' this error after running 'ls -l /usr/local/bin/python*' – Shaheem PP Sep 15 '22 at 15:06
  • 1
    I tried many solutions and this was the only one that worked - thank you so much!!! – Display name Nov 21 '22 at 15:31
36

Go to terminal type:

alias python=python3.x

This will setup default python as python3.x

Aditya Malviya
  • 1,907
  • 1
  • 20
  • 25
21

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

alias python=/usr/local/bin/python3
toadead
  • 1,038
  • 16
  • 29
17

The following worked for me

cd /usr/local/bin
mv python python.old
ln -s python3 python
Mayank Jain
  • 2,995
  • 2
  • 23
  • 19
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?

user3071284
  • 6,955
  • 6
  • 43
  • 57
CT Zhu
  • 52,648
  • 17
  • 120
  • 133
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.

Mr. Crowley
  • 3,225
  • 4
  • 25
  • 28
11
$ sudo ln -s -f $(which python3) $(which python)

done.

oori
  • 5,533
  • 1
  • 30
  • 37
10

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
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.

jacob
  • 398
  • 4
  • 13
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.

ArunDhwaj IIITH
  • 3,833
  • 1
  • 24
  • 14
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

doubleOK
  • 353
  • 6
  • 19
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!

Vikas Pandey
  • 197
  • 7
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

ChamCham
  • 484
  • 1
  • 8
  • 19
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
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
PeterT
  • 920
  • 8
  • 20
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.

Vorticity
  • 4,582
  • 4
  • 32
  • 49
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)".

E_K
  • 2,159
  • 23
  • 39
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.

Istiaque Hossain
  • 2,157
  • 1
  • 17
  • 28
Clark So
  • 19
  • 1
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.

Tor_Gash
  • 164
  • 1
  • 9