8

I have just installed Python 3.5.1 on my Mac (running the latest version of OSX). My system came with Python 2.7 installed. When I type IDLE at the Terminal prompt my system pulls up the original Python 2.7 rather than the newly installed Python 3.5. How do I get my system to default to Python 3.5.1 when I open the IDLE window from Terminal?

smci
  • 32,567
  • 20
  • 113
  • 146
user3798654
  • 431
  • 2
  • 9
  • 19
  • 1
    Some answers say uninstall it. If this works for OSX, great, enjoy. If you run Debian/Ubuntu removing Python2 will hose your machine as too many system components expect it to be there. – msw Dec 30 '15 at 11:06

7 Answers7

14

Since Python 2 and 3 can happily coexist on the same system, you can easily switch between them by specifying in your commands when you want to use Python 3.

So for Idle, you need to type idle3 in the terminal in order to use it with Python 3 and idle for using it with Python 2.

Similarly, if you need to run a script or reach a python prompt from the terminal you should type python3 when you want to use Python 3 and python when you want to use Python 2.

Railslide
  • 5,344
  • 2
  • 27
  • 34
4

It's good practice to have your MacOS Python environment set up properly from the beginning making sure that Homebrew installations take precedence over stock MacOS binaries. You want it in usr/local/bin not MacOS default usr/bin.

.bash_profile

# Ensure user-installed binaries take precedence
export PATH=/usr/local/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc

Can also create aliases for both.

alias py2='python2.7'
alias py3='python3.6'

Source the file to ensure it takes effect for the current session

source ~/.bash_profile

Homebrew install and setup etc...

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew doctor
brew update
brew upgrade --all
brew cleanup

Python3 install

brew install python3

Next

pip3 install virtualenv

Next

pip3 install virtualenvwrapper

When all is finished python3, pip3, virtualenv, and virtualenvwrapper.sh will all be in usr/local/bin.

Result

Every time I install anything or use commands like mkvirtualenv Python 3 is used by default.

Edison
  • 11,881
  • 5
  • 42
  • 50
3

You can use the python3 command (instead of using python), or you can simply uninstall the 2.7 version if you don't use it

  • Hello, Thank you for your response. I am not clear on how to uninstall 2.7. Where would I use the python3 command. To access python in idle I just type idle at the terminal prompt. – user3798654 Dec 30 '15 at 11:14
  • 10
    Do not remove the Python2.7 that comes with OS-X. It depends on it. – Roland Smith Dec 30 '15 at 13:05
  • The Mac OS needs Python 2.7 and you can't uninstall it. Maybe you can uninstall if the 2.7 was installed with `brew` or similar tools. – Arefe Feb 28 '19 at 07:52
1

If you dont have any python 2 scripts that you use, you can delete python2. But its not a problem to have them both installed. You just have to use another path python3 to launch IDLE.

I would prefer to let them both installled so if you have any scripts that are in python 2 you can still run them or you have to port them to python3.

msw
  • 42,753
  • 9
  • 87
  • 112
  • Hi Thank you for your response. How do I use another path python3 to launch IDLE? Thanks, George – user3798654 Dec 30 '15 at 11:15
  • Because I have to use both for boring reasons `alias p2=/path/to/python2` and `alias p3`. in `~/.bashrc` or `~/.bash_aliases` is both explicit and saves typing. I'm also fond of ipython so `ip2` and `ip3` are aliases also. – msw Dec 30 '15 at 11:21
  • I don't understand you post. Would you please expand on your explanation. Thanks, George – user3798654 Dec 30 '15 at 11:26
1

You can switch to any python version in your project by creating a virtual environment.

  • virtualenv -p /usr/bin/python2.x (or python 3.x)

In case you just want to run a program in a specific version just open shell and enter python2.x or python3.x

Dry_accountant_09
  • 1,371
  • 16
  • 15
1

Do right thing, do thing right!

  1. Open your terminal,

  2. input python -V, It likely shows:Python 2.7.10

  3. input python3 -V, It likely shows:Python 3.7.2

  4. input where python or which python, It likely shows:/usr/bin/python

  5. input where python3 or which python3, It likely shows: /usr/local/bin/python3

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

  1. input source ~/.bash_profile under Bash or source ~/.zshrc under zsh.

  2. Quit the terminal.

  3. Open your terminal, and input python -V, It likely shows: Python 3.7.2

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

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

Hope this helped you all!

egg
  • 115
  • 1
  • 9
Vittore Marcas
  • 1,007
  • 8
  • 11
0

By typing python, you are actually referring to a link. You will find its location with $ which python. In my case it was /usr/local/bin/python. go there $open /usr/local/bin/ and just delete the original python, python-config and idle as they are identical to the 2.7 files in the same folder. Then duplicate the 3.5 files and rename them to what you just deleted.

This also changes the default link other editors like Sublime_ReplPython use and updates it therefore to the 3.5 Version. This was my major concern with the standard installation.

0-_-0
  • 1,313
  • 15
  • 15