Installed Python 3 on Mac OS X but its still Python 2.7
Asked Answered
M

9

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?
Minimus answered 7/3, 2015 at 23:32 Comment(0)
A
192

Try typing python3 instead of just python.

Acidulate answered 7/3, 2015 at 23:33 Comment(0)
S
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.

Sibling answered 16/10, 2016 at 14:13 Comment(0)
O
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

Ostia answered 29/7, 2019 at 11:13 Comment(8)
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.Clean
Thanks. pyenv works well, but be sure to shim it to the front of your PATH like so github.com/pyenv/pyenv#understanding-pathUnderhanded
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: github.com/pyenv/pyenv/issues/849Maharanee
@DannyBullis Thanks. Your comment helped me resolve my issue where python -V was still showing version 2Roxannroxanna
Unfortunately even following the eval "$(pyenv init -)" advice, python -V still results in Python 2.7.16 instead of the just installed 3.9.7Glowworm
@Glowworm 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 TerminalHelio
It works for Mac as mentioned by @HelioGrandioso
Answer by @Helio working as of 2022 (macOS Monterey 12.6.1) Thanks!Perversity
T
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
Tandratandy answered 22/5, 2019 at 0:4 Comment(1)
I added & to end so shell is free to use... alias python="python3&" , alias idle="idle3&"Economic
J
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'.

Jagir answered 5/10, 2016 at 14:36 Comment(0)
C
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)
Cockswain answered 31/5, 2018 at 9:17 Comment(0)
F
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

Fraga answered 1/9, 2020 at 5:6 Comment(0)
E
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

Ebeneser answered 30/9, 2020 at 16:19 Comment(0)
B
-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.

Burkhardt answered 26/6, 2020 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.