Home > programming, python > Install Python 2.7.x under Snow Leopard / Lion (64 bits)

Install Python 2.7.x under Snow Leopard / Lion (64 bits)

February 13th, 2012 Leave a comment Go to comments

OS X comes with a Python installation that I find a bit confusing and misleading. What I present in this post is a guide to remove the default Python from your 64 bits OS X (i.e. Snow Leopard or Lion), install its latest version, and set up a nice environment to work with (e.g. iPython + MacVim). The only requirement for this process is to have the latest version of XCode, that you can get from the App Store.

Uninstall Python

I couldn’t find any nice and clean uninstaller for Python in OS X. So we will have to remove all files by hand. Here is the code:

sudo rm -rf /Library/Frameworks/Python.framework
sudo rm -rf /Applications/Python*
sudo rm -rf /Library/Python*
cd /usr/local/bin
sudo rm -rf 2to3 idle idle2.7 pydoc pydoc2.7 python* smtpd.py smtpd2.7.py
cd /usr/bin
sudo rm -rf 2to3 idle idle2.7 pydoc pydoc2.7 python* smtpd.py smtpd2.7.py

(if you have another versions of Python installed (e.g. 2.6, 2.5), just change 2.7 for your version number when removing the files. However, that is not really that important, just so that you don’t have that much garbage in your computer).

Install Python 2.7.x

Now let’s install the latest release of 2.7.x for OS X. You can see the latest releases here. When writing this post, the latest version is 2.7.2.

Once you have run the installer, edit your ~/.bash_profile file. To edit your file, use your favorite text edito (vim in my case):

vim ~/.bash_profile

You should now have the following lines (add them if you don’t, but the installer is supposed to add them for you):


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

Now let’s add the compiler flags for 64-bits and the Python Path for the modules. This is important, otherwise your easy_install will probably fail. Add the following lines to your ~/.bash_profile:

export PYTHONPATH=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
export CFLAGS="-I/Developer/SDKs/MacOSX10.7.sdk/usr/lib/"
export ARCHFLAGS="-arch x86_64"

(note: change MacOSX10.7 to MacOSX10.6 if you’re using Snow Leopard in the second line)

Notice that all your modules will be installed here:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. It’s kind of important to remember this path, so you may want to add it to your sidebar in Finder or create some soft link.

Close your file and restart your bash so that the new environment variables are available. Check that you can now run python from the new terminal.

Install most common modules

I usually use easy_install to install new modules. You can get it from here (the module is called setuptools).

Download it, decompress it, and type the following from it’s folder:

python setup.py build
python setup.py install

Now you can use easy_install to install your favorite modules. E.g.:

easy_install numpy
easy_install scipy
easy_install matplotlib
easy_install ipython

If you’re running Lion, I highly recommend the SciPy SuperPack, which will do all the work for you.

Setting up your programming environment

This last section is completely biased due to my eternal love to VIM. I know some heartless people who use Emacs, or even Eclipse (I was one of them, and it was a terrible thing!) to code in Python.

If you’re a VIM lover you should already know about MacVim, but if you don’t you should definitively get it.

In any case, you should choose your favorite text editor (in case you want to use VIM -everybody wants to use VIM, but unfortunately some people don’t know that-, here it is a nice python module), and then I highly recommend using iPython instead of the classic Python console. This is especially useful for debugging and plotting. iPython is life changing, really.

To run iPython with matplotlib already running in the background just type:

ipython --pylab

Finally, here is a nice screenshot of how my programming environment looks like (iPython + PyLab + MacVim):

Beautiful isn’t it? : )

And finally, for something -not that- completely different: Happy programming!

  • Facebook
  • Twitter
  • Google Plus
  • Meneame
  • Add to favorites
  • RSS
  • BarraPunto
Categories: programming, python Tags:
  1. slflow
    February 22nd, 2013 at 16:00 | #1

    Hi Uri,

    After running into multiple issues installing numpy/scipy on OSX 10.8, I attempted your method but am still running into problems. After following your instructions for reinstalling python, once I attempt to run easy_install using python setup.py build I get the following error: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can’t open file ‘setup.py’: [Errno 2] No such file or directory

    Any tips on resolving this issue?

    Cheers!

  2. February 23rd, 2013 at 11:58 | #2

    Do you get this error when running “easy_install” or when running “python setup.py build”? If the latter, make sure that in your current folder you have a file named “setup.py”.

    In any case, what are you trying to install with easy_install?

  3. morgan
    September 27th, 2013 at 17:02 | #3

    super helpful! thanks so much for posting this.

  4. Krista
    July 18th, 2014 at 12:52 | #4

    Having issues with this… I successfully uninstalled python. And I edited the bash_profile.

    But, when I closed out of terminal and then reopened it and attempted to view the bash_profile again, I got this:

    E325: ATTENTION
    Found a swap file by the name “~/.bash_profile.swp”
    owned by: Krista dated: Fri Jul 18 09:47:23 2014
    file name: ~Krista/.bash_profile
    modified: YES
    user name: Krista host name: Kristas-MacBook-Pro.local
    process ID: 17093
    While opening file “/Users/Krista/.bash_profile”
    dated: Thu Jul 17 13:20:15 2014

    (1) Another program may be editing the same file.
    If this is the case, be careful not to end up with two
    different instances of the same file when making changes.
    Quit, or continue with caution.

    (2) An edit session for this file crashed.
    If this is the case, use “:recover” or “vim -r /Users/Krista/.bash_profile”
    to recover the changes (see “:help recovery”).
    If you did this already, delete the swap file “/Users/Krista/.bash_profile.s
    wp”
    to avoid this message.

    Swap file “~/.bash_profile.swp” already exists!

    I was able to open the file as read only, but it reverted to its original and I don’t know what to do? Help!

  5. Krista
    July 18th, 2014 at 13:00 | #5

    @Krista Also, just adding more, when I recovered, I got the bash_profile back (with the edits), but now when it recovers it gives me the original. I’m really confused here…

  1. No trackbacks yet.


+ 2 = eight