222

I have installed Python 3.2 on my Mac. After I run /Applications/Python 3.2/Update Shell Profile.command, it's confusing that when I type python -V in Terminal, it says Python 2.6.1 which is not what I expected.

How can I change the default Python version?

2

22 Answers 22

181

[updated for 2021]

(Regardless if you are on Mac, Linux, or Windows:)

If you are confused about how to start the latest version of python, on most platforms it is the case that python3 leaves your python2 installation intact (due to the above compatibility reasons); thus you can start python3 with the python3 command.

Historically...

The naming convention is that generally, most scripts will call python2 or python3 explicitly. This happened due to a need for backwards compatibility.

Even though technically python doesn't even guarantee backwards compatibility between minor versions, Python3 really breaks backwards compatibility. At the time, programs invoking 'python' were expecting python2 (which was the main version at the time). Extremely old systems may have programs and scripts which expect python=python2, and changing this would break those programs and scripts.

At the time this answer was written, OP should not have changed this due to maintaining compatibility for old scripts.

Circa year 2021...

Nowadays, many years after the python2->python3 transition, most software explicitly refers to python2 or python3 (at least on Linux). For example, they might call #!/usr/bin/env python2 or #!/usr/bin/env python3. This has for example (python-is-python3-package) freed up the python command to be settable to a user default, but it really depends on the operating system.

The prescription for how distributions should handle the python command was written up in 2011 as PEP 394 -- The "python" Command on Unix-Like Systems. It was last updated in June 2019.

Basically if you are writing a library, you should specify the version of python (2 or 3, or finer-grained under specific circumstances) you can use. Otherwise as an end user, you should feel free to rename this for your own personal use (though your OS or distribution may not make that easy).

Shell alias:

You could, however, make a custom alias in your shell. The way you do so depends on the shell, but perhaps you could do alias py=python3, and put it in your shell startup file (.bashrc, .zshrc, etc). This will only work on your local computer (as it should), and is somewhat unnecessary compared to just typing it out (unless you invoke the command constantly).

Confused users should not try to create aliases or virtual environments or similar that make python execute python3; this is poor form.This is acceptable nowadays, but PEP 394 suggests encouraging users to use a virtualenv instead.

Different 3.* versions, or 2.* versions:

In the extremely unlikely case that if someone comes to this question with two python3 versions e.g. 3.1 vs 3.2, and you are confused that you have somehow installed two versions of python, this is possibly because you have done manual and/or manual installations. You can use your OS's standard package/program install/uninstall/management facilities to help track things down, and perhaps (unless you are doing dev work that surprisingly is impacted by the few backwards-incompatible changes between minor versions) delete the old version (or do make uninstall if you did a manual installation). If you require two versions, then reconfigure your $PATH variable so the 'default' version you want is in front; or if you are using most Linux distros, the command you are looking for is sudo update-alternatives. Make sure any programs you run which need access to the older versions may be properly invoked by their calling environment or shell (by setting up the var PATH in that environment).

A bit about $PATH

sidenote: To elaborate a bit on PATH: the usual ways that programs are selected is via the PATH (echo $PATH on Linux and Mac) environment variable. You can always run a program with the full path e.g. /usr/bin/🔳 some args, or cd /usr/bin then ./🔳 some args (replace blank with the 'echo' program I mentioned above for example), but otherwise typing 🔳 some args has no meaning without PATH env variable which declares the directories we implicitly may search-then-execute files from (if /usr/bin was not in PATH, then it would say 🔳: command not found). The first matching command in the first directory is the one which is executed (the which command on Linux and Mac will tell you which sub-path this is). Usually it is (e.g. on Linux, but similar on Mac) something like /usr/bin/python which is a symlink to other symlinks to the final version somewhere, e.g.:

% echo $PATH
/usr/sbin:/usr/local/bin:/usr/sbin:usr/local/bin:/usr/bin:/bin

% which python
/usr/bin/python
% which python2
/usr/bin/python2
% ls -l /usr/bin/python
lrwxrwxrwx 1 root root 7 Mar  4  2019 /usr/bin/python -> python2*
% ls -l /usr/bin/python2  
lrwxrwxrwx 1 root root 9 Mar  4  2019 /usr/bin/python2 -> python2.7*
% ls -l /usr/bin/python2.7
-rwxr-xr-x 1 root root 3689352 Oct 10  2019 /usr/bin/python2.7*

% which python3         
/usr/bin/python3
% ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 9 Mar 26  2019 /usr/bin/python3 -> python3.7*
% ls -l /usr/bin/python3.7
-rwxr-xr-x 2 root root 4877888 Apr  2  2019 /usr/bin/python3.7*

% ls -l /usr/bin/python*
lrwxrwxrwx 1 root root       7 Mar  4  2019 /usr/bin/python -> python2*
lrwxrwxrwx 1 root root       9 Mar  4  2019 /usr/bin/python2 -> python2.7*
-rwxr-xr-x 1 root root 3689352 Oct 10  2019 /usr/bin/python2.7*
lrwxrwxrwx 1 root root       9 Mar 26  2019 /usr/bin/python3 -> python3.7*
-rwxr-xr-x 2 root root 4877888 Apr  2  2019 /usr/bin/python3.7*
lrwxrwxrwx 1 root root      33 Apr  2  2019 /usr/bin/python3.7-config -> x86_64-linux-gnu-python3.7-config*
-rwxr-xr-x 2 root root 4877888 Apr  2  2019 /usr/bin/python3.7m*
lrwxrwxrwx 1 root root      34 Apr  2  2019 /usr/bin/python3.7m-config -> x86_64-linux-gnu-python3.7m-config*
lrwxrwxrwx 1 root root      16 Mar 26  2019 /usr/bin/python3-config -> python3.7-config*
lrwxrwxrwx 1 root root      10 Mar 26  2019 /usr/bin/python3m -> python3.7m*
lrwxrwxrwx 1 root root      17 Mar 26  2019 /usr/bin/python3m-config -> python3.7m-config*

sidenote2: (In the rarer case a python program invokes a sub-program with the subprocess module, to specify which program to run, one can modify the paths of subprocesses with sys.path from the sys module or the PYTHONPATH environment variable set on the parent, or specifying the full path... but since the path is inherited by child processes this is not remotely likely an issue.)

5
  • 184
    This is helpful information, but the question was "how can I change the default python version." Can we add something to help people trying to do that?
    – dmeyerson
    Sep 2, 2016 at 14:27
  • 1
    On Mac installing Python3 leaves Python2 intact as well. But it seems to me there is a way to change the default that is run when just "python" is typed. I came across the option once. I have chosen to leave it as is where "python --version" shows 2.7.12 and "python3 --version" shows 3.7.0. I agree with dmeyerson above that the question is how to change it. Your advice is good but there is a way to change it if you must (or really, really want to). Aug 15, 2018 at 3:41
  • 5
    > The answer you are probably looking for is You should not change this. And here I thought it was "how do I change this"! May 6, 2019 at 19:18
  • 3
    This answer is correct. Do not change what python points to at the system level as you can break important components of you OS if you do. Instead, use a separate Python 3 installation and virtualen to give yourself a “local” default Python.
    – Martijn Pieters
    Sep 24, 2019 at 23:20
  • 1
    "In the extremely unlikely case that if someone comes to this question with two python3 versions" -- I don't think this is as unlikely as you make it sound, since that is why I came to this question. Some custom code requires the use of older versions of python. Aug 22, 2023 at 0:26
137

Check the location of python 3

$ which python3
/usr/local/bin/python3

Write alias in bash_profile

vi ~/.bash_profile  
alias python='/usr/local/bin/python3'

Reload bash_profile

source ~/.bash_profile

Confirm python command

$ python --version
Python 3.6.5
3
  • 15
    this is a bad solution. As mentioned in the accepted answer, $ python is reserved for python 2. Any program you run on your computer that expects python 2 would then receive python 3 due to the alias, which could have unforeseen negative consequences.
    – rykener
    Mar 5, 2019 at 3:42
  • 1
    I think that source is only compatible with the Bourne command line. For greater compatibility it would be . instead Sep 15, 2021 at 19:11
  • Best solution for me. I just changed a little bit, instead of ~/.bash_profile I changed ~/.bashrc (remember to make a backup cp .bashrc bashrc_bkp before changing it!) Mar 28 at 22:25
56

Old question, but alternatively:

virtualenv --python=python3.5 .venv
source .venv/bin/activate
3
  • sure but if you don't have the executable (e.g. python3.5) this solution doesn't work. Nov 23, 2016 at 13:22
  • I think this covers that stackoverflow.com/questions/5506110/…
    – FreshPow
    Nov 28, 2016 at 22:25
  • Simple and brilliant solution, thanks. No need to go through conda, pyenv etc. as in other answers.
    – Phys
    Nov 27, 2022 at 18:15
50

On Mac OS X using the python.org installer as you apparently have, you need to invoke Python 3 with python3, not python. That is currently reserved for Python 2 versions. You could also use python3.2 to specifically invoke that version.

$ which python
/usr/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ cd /Library/Frameworks/Python.framework/Versions/3.2/bin/
$ ls -l
total 384
lrwxr-xr-x  1 root  admin      8 Apr 28 15:51 2to3@ -> 2to3-3.2
-rwxrwxr-x  1 root  admin    140 Feb 20 11:14 2to3-3.2*
lrwxr-xr-x  1 root  admin      7 Apr 28 15:51 idle3@ -> idle3.2
-rwxrwxr-x  1 root  admin    138 Feb 20 11:14 idle3.2*
lrwxr-xr-x  1 root  admin      8 Apr 28 15:51 pydoc3@ -> pydoc3.2
-rwxrwxr-x  1 root  admin    123 Feb 20 11:14 pydoc3.2*
-rwxrwxr-x  2 root  admin  25624 Feb 20 11:14 python3*
lrwxr-xr-x  1 root  admin     12 Apr 28 15:51 python3-32@ -> python3.2-32
lrwxr-xr-x  1 root  admin     16 Apr 28 15:51 python3-config@ -> python3.2-config
-rwxrwxr-x  2 root  admin  25624 Feb 20 11:14 python3.2*
-rwxrwxr-x  1 root  admin  13964 Feb 20 11:14 python3.2-32*
lrwxr-xr-x  1 root  admin     17 Apr 28 15:51 python3.2-config@ -> python3.2m-config
-rwxrwxr-x  1 root  admin  25784 Feb 20 11:14 python3.2m*
-rwxrwxr-x  1 root  admin   1865 Feb 20 11:14 python3.2m-config*
lrwxr-xr-x  1 root  admin     10 Apr 28 15:51 pythonw3@ -> pythonw3.2
lrwxr-xr-x  1 root  admin     13 Apr 28 15:51 pythonw3-32@ -> pythonw3.2-32
-rwxrwxr-x  1 root  admin  25624 Feb 20 11:14 pythonw3.2*
-rwxrwxr-x  1 root  admin  13964 Feb 20 11:14 pythonw3.2-32*

If you also installed a Python 2 from python.org, it would have a similar framework bin directory with no overlapping file names (except for 2to3).

$ open /Applications/Python\ 2.7/Update\ Shell\ Profile.command
$ sh -l
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ cd /Library/Frameworks/Python.framework/Versions/2.7/bin
$ ls -l
total 288
-rwxrwxr-x  1 root  admin    150 Jul  3  2010 2to3*
lrwxr-x---  1 root  admin      7 Nov  8 23:14 idle@ -> idle2.7
-rwxrwxr-x  1 root  admin    138 Jul  3  2010 idle2.7*
lrwxr-x---  1 root  admin      8 Nov  8 23:14 pydoc@ -> pydoc2.7
-rwxrwxr-x  1 root  admin    123 Jul  3  2010 pydoc2.7*
lrwxr-x---  1 root  admin      9 Nov  8 23:14 python@ -> python2.7
lrwxr-x---  1 root  admin     16 Nov  8 23:14 python-config@ -> python2.7-config
-rwxrwxr-x  1 root  admin  33764 Jul  3  2010 python2.7*
-rwxrwxr-x  1 root  admin   1663 Jul  3  2010 python2.7-config*
lrwxr-x---  1 root  admin     10 Nov  8 23:14 pythonw@ -> pythonw2.7
-rwxrwxr-x  1 root  admin  33764 Jul  3  2010 pythonw2.7*
lrwxr-x---  1 root  admin     11 Nov  8 23:14 smtpd.py@ -> smtpd2.7.py
-rwxrwxr-x  1 root  admin  18272 Jul  3  2010 smtpd2.7.py*
2
  • Regarding "currently reserved for Python 2 versions," do you have a link for that?
    – Wildcard
    Jul 2, 2016 at 4:15
  • 3
    If i have python 3.4 and python 3.7, when I type python3 from terminal, it executes python 3.4 not 3.7, how to fix this?
    – Ysh
    Jul 10, 2019 at 18:19
43

Update 11 May 2022 Wed EST PM 06:36

Thanks @Aditya Deshpande! Great suggestion!

I've just fixed my answer.

After taking ~/.bashrc or ~/.zshrc effect, there is no need to quit and restart the terminal.


Do right thing, do thing right!

--->Zero Open your terminal,

--Firstly input python -V, It likely shows:

Python 2.7.10

-Secondly input python3 -V, It likely shows:

Python 3.7.2

--Thirdly input where python or which python, It likely shows:

/usr/bin/python

---Fourthly input where python3 or which python3, It likely shows:

/usr/local/bin/python3

--Fifthly add the following line at the bottom of your PATH environment variable file in ~/.profile file or ~/.bash_profile under Bash or ~/.zshrc under zsh.

alias python='/usr/local/bin/python3'

OR

alias python=python3

-Sixthly input source ~/.bash_profile under Bash or source ~/.zshrc under zsh.

--Seventhly then checkout the version of Python




input python -V, It likely shows:

Python 3.7.2

I had done successfully try it.

Others, the ~/.bash_profile under zsh is not that ~/.bash_profile.

The PATH environment variable under zsh instead ~/.profile (or ~/.bash_file) via ~/.zshrc.

Help you guys!

2
  • 2
    Thanks for the guide, however, when you run source ~/.bash_profile or source ~/.zshrc, you don't need to quit and restart the terminal for your change to take effect. May 11, 2022 at 21:30
  • Thanks @Aditya Deshpande! Great suggestion! I've just fixed my answer. May 12, 2022 at 4:02
22

Change the "default" Python by putting it ahead of the system Python on your path, for instance:

export PATH=/usr/local/bin:$PATH
2
  • My assumption is that by doing this you could break something on Mac. Since there are many programs and scripts which use the "python" alias. If suddenly, because of the priority change in PATH, they begin to point toward Python3 and not Python2 - that could break something. Potentially the entire OS...that's my assumption though and not tested.
    – Aivoric
    Jul 31, 2015 at 8:35
  • 4
    Any system software would be using the full path to the system provided Python. Jul 31, 2015 at 18:52
20

Set Python 3.5 with higher priority

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2

Check the result

sudo update-alternatives --config python
python -V
1
  • This helped me to fix the problem, a lot of softwares were malfunctioning as the priority of python2 was higher,
    – Seungju
    Sep 21, 2021 at 20:00
10

Check the execution path of python3 where it has libraries

$ which python3
/usr/local/bin/python3  some OS might have /usr/bin/python3

open bash_profile file and add an alias

vi ~/.bash_profile  
alias python='/usr/local/bin/python3' or alias python='/usr/bin/python3'

Reload bash_profile to take effect of modifications

source ~/.bash_profile

Run python command and check whether it's getting loading with python3

$ python --version
Python 3.6.5
4

If you've installed Python with the brew package manager, you can do this:

brew link --overwrite [email protected]
1
  • For some reason didnt work for me, and brew offered the following solution which is very similar: brew unlink python@<current_version> && brew link python@<wanted_version>
    – SaharCo
    Mar 24 at 8:13
2

According to a quick google search, this update only applies to the current shell you have open. It can probably be fixed by typing python3, as mac and linux are similar enough for things like this to coincide. Link to the result of google search.

Also, as ninjagecko stated, most programs have not been updated to 3.x yet, so having the default python as 3.x would break many python scripts used in applications.

1
  • This, by the way, was true in 2011, but far from true in 2022+ , but those coming in;- before yelling at poster, note the date this was posted.
    – Shayne
    Dec 9, 2022 at 11:31
2

I am using OS X 10.7.5 and Python 3.4.2. If you type python3 and what you want to run it will run it using python 3. For example pyhton3 test001.py. That ran a test program I made called test001. I hope this helps.

2
  • 5
    Welcome to Stack Overflow! It looks like your answer isn't a real answer to the question as asked. Please refer to stackoverflow.com/help/how-to-answer for help if you want to fix it.
    – Others
    Feb 28, 2015 at 22:05
  • I spent HOURS trying to get python3 to work on my Mac--turned out typing 'python3' instead of the old 'python' did the trick.
    – YCode
    Dec 31, 2017 at 21:03
2
sudo mv /usr/bin/python /usr/bin/python2
sudo ln -s $(which python3) /usr/bin/python

This will break scripts, but is exactly the way to change python. You should also rewrite the scripts to not assume python is 2.x. This will work regardless of the place where you call system or exec.

2

you can change temporarily or switch between different versions using following commands:

set path=C:\Users\Shaina\AppData\Local\Programs\Python\Python35-32;%PATH%
python --version

enter image description here

2

It should be noted that recent versions of Homebrew/MacOS will require a different entry for the PATH as the location where Homebrew installs Python has changed. Add this like to your .zshrc:

export PATH="/opt/homebrew/opt/python/libexec/bin:$PATH"

This will ensure that the appropriate unversioned python link installed by Homebrew appears before that of version 2.x and will protect you from being impacted by any version updates to python as brew will update the unversioned links whenever it updates the version.

2

Starting with macOS Catalina the default shell is zsh. Therefore, all those ~/.bash_profile changes are not going to change the default when you open a new terminal since the new terminal is a zsh shell and not a bash shell.

You can confirm your terminal is a zsh shell by typing echo $SHELL and you should see a response of: zsh.

What should you do? You should use a solution that works for zsh shell and bash shell. Therefore, do the following:

  1. enter: vi ~/.bash_profile
  2. enter: alias python='python3'
  3. close and save your bash_profile. Enter: :wq
  4. Now open/create your zsh profile. Enter: vi ~/.zshrc
  5. source your bash inside your zshrc. Enter: source ~/.bash_profile

Now python will be aliased to python3 in your zsh shells (and in bash if you switch the default) automatically.

Test: python --version

Python 3.8.9

2
  • I run a program called odoo in virtual env using: ~/odoo-env/bin/activate and then python ./odoo.py. At some point I updated to v3 and have both on my macos but when i run it now I get this /usr/local/bin/python3: can't open file '/odoo.py' if i have the alias in bash_profile and if i remove the alias I get this: Library not loaded: @executable_path/../.Python Referenced from: /Users/mars/odoo-env/bin/python2.7 Reason: tried: '/Users/mars/odoo-env/bin/../.Python' (no such file), '/usr/local/lib/.Python' (no such file), '/usr/lib/.Python' (no such file) Abort trap: 6 pls hlp
    – marciokoko
    Feb 10, 2022 at 19:02
  • I just noticed that I dont have a .Python in my usr/lib or usr/local/lib. What I have is a python2.7 in the former and python 2.7 and python3.9 in the latter.
    – marciokoko
    Feb 10, 2022 at 22:22
2

I had a problem updating my python version on my Mac and tried everything to installed it.

  1. First I checked the version: python --version
  2. Then I checked the path: echo $PATH

I checked the path and then I realized that the python version my Mac was using was the one in the miniconda package I previously installed.

So I just used this command: conda install python=$pythonversion$ with the version I wanted to install.

Then when it finished, I checked again the version and it was finally updated.

1

Navigate to:

My Computer -> Properties -> Advanced -> Environment Variables -> System Variables

Suppose you had already having python 2.7 added in path variable and you want to change default path to python 3.x

then add path of python3.5.x folder before python2.7 path.

open cmd: type "python --version"

python version will be changed to python 3.5.x

2
  • 3
    That's Windows. Nov 9, 2018 at 19:58
  • Some IDEs thas includes terminals/cmds/consoles, propagates the environment variables. Restart the IDE to get the changes inside the embedd terminal. Nov 27, 2018 at 17:20
1

In short: change the path in Environment Variables!

For Windows:

  • Advanced System Settings > Advance (tab). On bottom you'll find 'Environment Variables'

  • Double-click on the Path. You'll see path to one of the python installations, change that to path of your desired version.

1

In my case, on my Mac OSX, with Python 2.7.18 installed via mac ports, I was able to set the python version to 2.7 with:

$ sudo port select --set python python27

So:

$ python -V
Python 2.7.18
1
  • Also, MacPorts does not setup python2 and python3 commands. You can do this with port select like: sudo port select --set python3 python37.
    – Mat Gessel
    Dec 12, 2020 at 0:00
0

As of 2024

When you download the Python package from python.org and install, the installer updates your .bash_profile in order to appropriately set the PATH variable, like this:

# Setting PATH for Python 3.12
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.12/bin:${PATH}"
export PATH
$ which python3
/Library/Frameworks/Python.framework/Versions/3.12/bin/python3
$ ls -l /Library/Frameworks/Python.framework/Versions
drwxrwxr-x  11 root  admin  352 Aug 21  2023 3.11
drwxrwxr-x  11 root  admin  352 Mar 11 14:30 3.12
drwxrwxr-x  11 root  wheel  352 Mar  5  2020 3.8
lrwxr-xr-x   1 root  wheel    4 Mar 11 14:30 Current -> 3.12
-1

In order to change the python version context switching without exporting environment variable. Please use the below video link to see: https://youtu.be/jTN4MHNhJZs

-4

After installing the newer version of python to your computer...

When you want to run a python program (e.g. 'program.py') from the terminal (using the latest version of python on your system); instead of running 'python program.py' run 'python3 program.py'

Similarly, if you want to use python in the terminal (using the latest version of python on your system) run 'python3' instead of 'python'

As a test try to run 'python3 --v' in the terminal...

4
  • 1
    Please re-read the question: It does not ask How do I run a Python program?
    – greybeard
    Jun 10, 2021 at 19:08
  • I made the following assumptions: how do I change the default version of python in the terminal -- is implicitly equivalent to -- how do I change to the default version of python (to the latest version of python on my system) in the terminal -- is implicitly equivalent to -- how do I, by default, use the latest version of python in the terminal -- is implicitly equivalent to -- how do I run python programs through the terminal using the latest version of python on my system - THIS is the question I answered. I did NOT answer - How do I run a python program?
    – D_H
    Jun 11, 2021 at 12:35
  • "How to change default Python version?" is clearly not the same question as "how do I run python programs through the terminal using the latest version of python on my system.". DEFAULT is the key term here. This has very significant implications in doing things like how altering how pkg-config reports header locations, which pythons are used by shell scripts, etc etc. None of which is "how to latest python via terminal" I get it, your new, you want to be helpful. But this isn't the way. I strongly advise if you are a beginner, to read and not write.
    – Shayne
    Dec 9, 2022 at 11:27
  • Thanks, for the derogatory advice. I learned my lesson, don't provide tangential information. I still feel that people approaching this question are often looking for a way to run a certain version of python through the terminal. But you're right. You are the expert. I should not speak/write/contribute as a 'beginner'. Thank you.
    – D_H
    Dec 10, 2022 at 12:44

Your Answer

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

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