How to set Python's default version to 3.x on OS X? [duplicate]
Asked Answered
I

21

491

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?

Ivan answered 25/8, 2013 at 3:29 Comment(2)
You can manage the version of python with pyenv.Ennius
#49704864Rostock
S
663

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.

Shirashirah answered 25/8, 2013 at 4:6 Comment(16)
Should this not be put in ~/.bash_profile instead of ~/.bash_aliases?Maida
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.Rundlet
@Rundlet run source ~/.bash_profile after edit ~/.bash_profile file.Floating
You can do the same for pip: alias pip='pip3.6'Velazquez
@surfer190 that will cause problem when running with sudo, isn't it?Shirashirah
@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?Leucas
Is this everything that is required for me to switch to python3 in my shell and applications?Boyd
@Boyd 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.Shirashirah
Shouldn't cause any issues with sudo but will cause issues if you don't source your alias. This is more of a workaround.Strop
It won't take until you close your terminal window after the change to the .bash_profile are made.Osgood
you should set the pip alias not to pip3.6 specifically, instead set it to pip3: ``alias pip='pip3'Bias
adding -> alias python='python3' in ~/.bash_profile file worked for me.Caracas
Would it still break things now in Catalina?Acknowledge
Nope, its bash thing. Nothing to do with the operating system.Shirashirah
Do I need to source ~/.bash_profile everytime i close and open my terminal? It says command not found after I re-opened my terminalBecky
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 explains them in depth as well as alternative solutions that avoid these problems.Julian
E
232

You can solve it by symbolic link.

unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python
Ennius answered 6/8, 2016 at 15:49 Comment(15)
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.Junker
@ChadBefus Thank you for your reply. I agree with your opinion. unlink is safer than rm.Ennius
Does it have any consequences for scripts that expect python to be python2.7?Participate
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.Animato
it does nothingHygiene
macOS: unlink: /usr/bin/python: Operation not permittedGoggles
@Goggles try to just run the second original line (i.e. ln -s... with /local/).Reglet
If your app requires python3 for running, you should edit symlinks not alias.Hamitic
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".Cawnpore
This did not work for me. After following this still python2 is invoked by default.Windflower
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.Shirashirah
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 permittedCadmar
This feels like the cleaner approach. I set mine to python 3.5 and it worked.Adduction
I had to close terminal and open a new one to get it working in macOSPhosphoresce
I had to do this in my case ln -s -f /usr/local/Cellar/[email protected]/3.8.11/bin /usr/local/bin/python3Whiles
C
158

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

Crevice answered 5/4, 2019 at 15:5 Comment(5)
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 --versionBeau
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.Becky
@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?Cinquefoil
if you already installed visual studio code it will be easy to use. code ~/.bash_profile instead of using. vi ~/.bash_profile.Hosfmann
Just this code worked for my Mac, thanksContumacy
D
149

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
Diptych answered 1/10, 2019 at 19:8 Comment(8)
Works great (Reference)Eris
"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.Orozco
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.Archetype
@Archetype Thanks you. I appreciate your input.Diptych
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.Zsa
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?Zsa
but I'm getting. 'zsh: no matches found: /usr/local/bin/python*' this error after running 'ls -l /usr/local/bin/python*'Extrovert
I tried many solutions and this was the only one that worked - thank you so much!!!Charleen
M
37

Go to terminal type:

alias python=python3.x

This will setup default python as python3.x

Multiversity answered 17/1, 2019 at 9:39 Comment(3)
This worked for me but using "alias python=python3"Dipteral
This will temporarily set python version for that terminal instance.Subedit
Add it to ~/.zshrc to persist change on macosMerrick
C
21

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

alias python=/usr/local/bin/python3
Click answered 1/7, 2019 at 19:39 Comment(0)
F
17

The following worked for me

cd /usr/local/bin
mv python python.old
ln -s python3 python
Flivver answered 5/1, 2019 at 20:58 Comment(0)
P
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?

Prim answered 25/8, 2013 at 3:32 Comment(0)
U
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.

Unapproachable answered 1/3, 2018 at 14:42 Comment(0)
R
11
$ sudo ln -s -f $(which python3) $(which python)

done.

Relativity answered 30/12, 2019 at 20:56 Comment(4)
ln: /usr/bin/python: Operation not permittedGardas
ln: found: No such file or directoryMenchaca
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_fileFlounce
@Flounce your comment is unclear. just try: sudo ln -s -f $(which python3) /usr/local/bin/pythonRelativity
T
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
Tape answered 2/5, 2020 at 13:57 Comment(7)
Not sure why you got a down vote. This worked for me! Thanks for posing this. I upvoted you to cancel that down vote ;)Valdavaldas
On Catalina (10.15.17), I get: Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.Orozco
this does not work for meSphygmoid
Error: Unknown command: switchCrypto
For me it looked like this: brew link [email protected]. Thanks for hints!Tigerish
This does not work for Python 3.10.5, please could you post a solution that is not dependent on brew?Zsa
On Monterey it gives me this error: Error: No such keg: /usr/local/Cellar/3.11.4Overture
U
9

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.

Unprofitable answered 11/6, 2020 at 14:49 Comment(0)
M
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.

Mayfair answered 9/7, 2020 at 17:7 Comment(0)
F
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

Framing answered 10/4, 2015 at 19:28 Comment(1)
It does not happenOecology
K
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!

Kotz answered 7/5, 2021 at 12:58 Comment(0)
D
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

Dyne answered 12/2, 2019 at 13:21 Comment(0)
D
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
Dornick answered 10/4, 2019 at 3:28 Comment(0)
D
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.

Dichlorodiphenyltrichloroethane answered 25/8, 2013 at 5:31 Comment(0)
P
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.

Plausible answered 15/7, 2019 at 10:40 Comment(1)
This was the only solution that worked for me on M1 MacEricaericaceous
E
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.

Ecumenicity answered 2/10, 2019 at 11:35 Comment(0)
E
0

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

Exosphere answered 17/7, 2018 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.