21

I just downloaded python 2.7 on my mac which runs OS X 10.6.8. The previous version of python installed was 2.6.

When I type python in the terminal it opens python version 2.6. I want it to open python 2.7

How do I make Python 2.7 open by default?

6
  • How did you install your python 2.7?
    – waitingkuo
    Mar 8, 2013 at 2:20
  • with the mac installer disk image. Would it be better to install it through the terminal? Mar 8, 2013 at 2:21
  • 5
    Type which -a python to locate all the python you've installed first.
    – waitingkuo
    Mar 8, 2013 at 2:24
  • 2
    that takes me to the directory usr/bin/python. The disk image installed python to the directory Applications/Python 2.7/ Mar 8, 2013 at 2:26
  • 1
    Open a new terminal window and try again.
    – Ned Deily
    Mar 8, 2013 at 2:29

4 Answers 4

20

The python.org installers for Python 2.x on OS X by default modify shell profiles (for the standard shells like bash and csh) to add its framework bin directory to the front of your shell path. Assuming you did not deselect the option during installation, there should now be the following in your .bash_profile file.

# Setting PATH for Python 2.7
# The orginal version is saved in .profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

But this profile is only executed by default when you launch a new terminal window; it won't apply to existing terminal sessions. So make sure you open a new one and then try again. If you are using a different shell, you may need to modify that shell's startup to do the equivalent.

The python.org installers for Python 3.x on OS X do not select the shell script modification option by default. You can enable it at installation or you can later run the Update Shell Profile.command file in the corresponding Python x.x folder in the Applications folder. Or you can just manually edit the right profile.

12

The easier solution is to install it via MacPorts:

sudo port install python_select
port search python # Search for version you are looking for
sudo port install python27
sudo port select --set python python27
6
  • No, that will not help for Pythons installed by the python.org installer. That only applies to Apple-supplied system Pythons.
    – Ned Deily
    Mar 8, 2013 at 2:36
  • The OP already has a version of Python 2.7 installed. Why would you suggest installing yet another version? There's nothing wrong with installing a MacPorts Python but you would still need to modify your PATH for that.
    – Ned Deily
    Mar 8, 2013 at 2:47
  • @Ned Deily, no even if python.org version was installed previously sudo port select --set python <version> will work fine. Just tried it with python 2.7 and 3.3.0. Mar 8, 2013 at 2:58
  • Yes, but you have previously modified your PATH while installing MacPorts. And, again, there is no reason to install MacPorts just to solve an initial PATH problem when installing the python.org Python 2.7. That's using a sledgehammer to squash a tiny bug.
    – Ned Deily
    Mar 8, 2013 at 3:01
  • Where can I install macports from? Mar 11, 2013 at 0:35
11

Add followings to your ~/.bash_profile

# Setting PATH for Python 2.7
PATH="/path/to/your/python2.7/bin:${PATH}"
export PATH

Save the file and reopen the terminal.

2
  • The equivalent should have happened automatically by default with the python.org OS X installer disk image.
    – Ned Deily
    Mar 8, 2013 at 2:31
  • 1
    ~ means your home directory. And there's a hidden file .bash_profile there. You can edit it by typing vim ~/.bash_profile.
    – waitingkuo
    Mar 8, 2013 at 2:38
4

Because my account does not have the admin permission. I work around to set the config in ~/.zshrc or ~/.bashrc. Now I give a example that I assume you installed python 3.7. If you installed the other version, just change the version will be fine.

  1. ~/.zshrc solution
  $ echo "alias python=/usr/local/bin/python3.7" >> ~/.zshrc
  $ source ~/.zshrc
  1. ~/.bashrc solution
    $ echo "alias python=/usr/local/bin/python3.7" >> ~/.bashrc
    $ source ~/.bashrc
  1. check the result
    $ python --version   
    # print result Python 3.7.1

The other solutions please refer to : https://opensource.com/article/19/5/python-3-default-mac

Your Answer

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

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