85

I am currently running OS X Yosemite (10.10.2) on my MacBook Pro... By default, Apple ships Python 2.7.6 on Yosemite.

Just downloaded and ran this installer for Python 3: python-3.4.3-macosx10.6.pkg

When I opened up my Terminal and typed in python, this is what came up:

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Question(s):

  1. Does anyone know where the Python 3.4.3 interpreter was installed?
  2. Do I need to uninstall Python 2.7.3 (if so, how do I go about doing this) before setting a global environmental variable such as PYTHON_HOME to the location of the installed Python 3.4.3?
abhi
  • 1,760
  • 1
  • 24
  • 40
PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144

9 Answers9

191

Try typing python3 instead of just python.

rhombidodecahedron
  • 7,693
  • 11
  • 58
  • 91
48

While @rhombidodecahedron's answer is concise and to-the-point and @Nacho Izquierdo addresses your first question perfectly, my answer aims to answer your second question in some more detail:

One should not uninstall Python 2.7 which comes with Mac OS X; it is supplied by Apple and is needed for applications running on OS X. It is stored in /System/Library/Frameworks/... If it is removed, Mac OS X will have to be reinstalled.

Hope that helps! And to reiterate answers given by @rhombidodecahedron and @Nacho Izquierdo, install Python 3.x separately and use python3 if you would like to use that version.

Python 2.7 is the standard, Python 3.x is the future.

abhi
  • 1,760
  • 1
  • 24
  • 40
31

What you should not do -

moving default python binary to an unused name

$ sudo mv /usr/bin/python /usr/bin/python2

and then moving the new binary to the default path

$ sudo mv $PATHTOBINARY/python3 /usr/bin/python

What could be done but also shouldn't not be done

Since I use zsh by default, I put the following into the .zshrc file:

$ echo "alias python=/usr/local/bin/python3.7" >> ~/.zshrc

If you are using the default Bash shell, you can append this same text to your .bashrc:

$ echo "alias python=/usr/local/bin/python3.7" >> ~/.bashrc

This will work but it is not the recommended way because making future updates to Python will be difficult. It means we have to manually download the new files since Python doesn't include a command-line way to update.

What is the right way

The basic premise of all Python development is to never use the system Python. You do not want the Mac OS X 'default Python' to be 'python3'.

Usage of pyenv to manage Python environments is recommended.

$ brew install pyenv

$ pyenv install 3.7.3

$ pyenv global 3.7.3

$ pyenv version

Refresh the current terminal and check

$ python -V

It should give Python 3.7.3

This way you are good to go.

For further reference - https://opensource.com/article/19/5/python-3-default-mac

Community
  • 1
  • 1
Vivek Kumar
  • 2,625
  • 2
  • 25
  • 33
  • What does it mean when this doesn't work? I'm trying to run for 3.5.1 and get this error: make: *** No targets specified and no makefile found. Stop. – 4thSpace Oct 02 '19 at 15:22
  • 1
    Thanks. pyenv works well, but be sure to shim it to the front of your PATH like so https://github.com/pyenv/pyenv#understanding-path – corysimmons Dec 04 '19 at 03:58
  • 22
    The "Refresh the current terminal" step wasn't quite doing it for me. `python -V` was still showing me `2.7.6`. To get this to work I had to initialize pyenv by adding `eval "$(pyenv init -)"` in my `~/.bash_profile`. I added it to the end of the file. I then re-sourced the bash profile and upon running `python -V` again I now correctly see `3.7.6`. This github issue resolution helped me: https://github.com/pyenv/pyenv/issues/849 – Danny Bullis Jan 27 '20 at 05:05
  • @DannyBullis Thanks. Your comment helped me resolve my issue where `python -V` was still showing version 2 – philomath Feb 17 '21 at 02:48
  • 2
    Unfortunately even following the `eval "$(pyenv init -)"` advice, `python -V` still results in Python 2.7.16 instead of the just installed 3.9.7 – du-it Sep 14 '21 at 09:53
  • 3
    @du-it not sure if you found a workaround, but I encountered the same issue and was able to resolve by adding the following to `~/.zprofile`: `export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)"` And then running `source ~/.zprofile` or restarting Terminal – ClayHerendeen Dec 21 '21 at 15:45
  • It works for Mac as mentioned by @ClayHerendeen – Jason LiLy Aug 12 '22 at 13:17
  • Answer by @ClayHerendeen working as of 2022 (macOS Monterey 12.6.1) Thanks! – mandmeier Nov 03 '22 at 19:59
12

Since I know I'll only use python3, I added these 2 lines to .bash_profile file:

alias python="python3" # to use python3 rather than python2.7
alias idle="idle3" # to use python3 idle rather than 2.7
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Sherif Shendidy
  • 129
  • 1
  • 4
3

In the version OS X El Capitan, you can find the interpreter in: /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4

By dragging this path into the Terminal and pressing enter you will be able to run this version.

To run it faster you can either create an alias by typing in the Terminal: alias python = 'python3.4'.

nachoiz
  • 41
  • 7
3

You can easily do this using pyenv which is a Simple Python Version Management. It allows one to set specific Python versions to run on specific directories or one can change your version before using shell

i.e.

$ pyenv install 2.7.6
$ pyenv install 2.6.8
$ pyenv local 2.7.6
$ pyenv versions
  system
  2.6.8
* 2.7.6 (set by /home/yyuu/.pyenv/version)
serasmus
  • 31
  • 4
3

After several hours of linking, unlinking, uninstalling and installing python to get a version callback python 3.8.5 rather than python 2.7 adding these 2 lines to .bash_profile file answered above by Sherif Shendidy is what worked for me.

alias python="python3" # to use python3 rather than python2.7 alias idle="idle3" # to use python3 idle rather than 2.7

Rob
  • 31
  • 2
2

I have encountered similar problem on windows as well.I would like to address this issue on windows. If you have already installed python 2 and python 3, Note: While installation make sure you click on "Add to environment variables" or "Add to path".

Check version in cmd: python -V

If it shows 2.x, windows > edit the system environment variables > environment variables > user variables > path click edit You should notice a bunch of directories in some order. Find a directory similar to below: C:\Users\some name\AppData\Local\Programs\Python\Python3.x\ click on Move up until this directory is on top. click ok. Now restart the cmd. type python -V you will find 3.x

Akhil s
  • 21
  • 4
-2

For windows users, I just deleted the folder containing the python2.7. I just reinstalled the python 3.x.x and after that reboot my laptop.