Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python being auto-aliased in Mac OS X Lion

I have been struggling with setting Python 2.7 to be the default Python on my system.

Python 2.6 came with my OS, and I have since installed 2.7 (from source) and have been trying to make that my default version.

At one point it was suggested that I try a package manager (MacPorts / Fink / Homebrew) and I did, and I think that may have been a bad idea.

The overall issue is that, somewhere, somehow, Python 2.6 is getting auto-aliased as my default python every time my shell starts. I've gone through my .profile and .bashrc, and there are no commands that could alias them.

I've also set my default path to look at /Library/Frameworks/Python.framework/Versions/2.7/bin first. I have also tried the suggestions in this SO post and this SU post to no avail. I need to use 2.7 as my default, because I have scripts that depend on argparse and so on.

Further, my /usr/bin/python is not aliased to anything, and /usr/local/bin is: /usr/local/bin/python -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python

like image 693
the_e Avatar asked Mar 15 '13 18:03

the_e


People also ask

Does Mac automatically come with Python?

Python comes pre-installed on Mac OS X so it is easy to start using. However, to take advantage of the latest versions of Python, you will need to download and install newer versions alongside the system ones.

How do I use Python 3 instead of python2 on Mac?

Open the terminal (bash or zsh) whatever shell you are using. Install python-3 using Homebrew (https://brew.sh). Look where it is installed. Change the default python symlink to the version you want to use from above.


1 Answers

I have been struggling with setting Python 2.7 to be the default Python on my system.

It already was. When you take a clear Lion box, and type python into a shell, it runs /usr/bin/python, which is Python 2.7.2.

Python 2.6 came with my OS

Lion comes with 2.5, 2.6, and 2.7, and a hidden 2.3.

They're installed in /System/Library/Frameworks/Python.framework/Versions. All but 2.3 have symlinks /usr/bin/python2.x that, on a clean system, will be the first thing on your PATH with that name, so just typing python2.6 will run 2.6, while python2.7 will run 2.7.

And there's also a special wrapper at /usr/bin/python that runs 2.7 by default, but you can configure that with VERSIONER_PYTHON_VERSION or the com.apple.versioner.python preference.

I have since installed 2.7 (from source) and have been trying to make that my default version.

What do you mean by "default version"? Do you just want it to be what gets run when you type python in a fresh Terminal shell, or use #!/usr/bin/env python as a shebang line in a script?

The easiest way to do that is to get whatever directory you installed it into higher on the PATH than /usr/bin.

If you've installed a "framework build" (which you should have), there will be a directory /Library/Frameworks/Python.framework/Versions/2.7/bin that you can put at the top of your PATH. This avoids affecting the order of /usr/local and /usr, and it means you can use any scripts that get installed by Python setup.py installations without having to symlink them into /usr/local/bin.

This is important, because Apple's Python will install scripts into /usr/local/bin—for example, if you /usr/bin/easy_install-2.7 pip, you'll get /usr/local/bin/pip and /usr/local/bin/pip-2.7. If you also installpip` for your custom-built Python, you don't want it to appear in the same place; otherwise, whichever you installed last replaces the other.

If you didn't install a framework build, or you've configured it to install scripts to /usr/local/bin anyway, or you want to affect the order of /usr/local, just put /usr/local/bin at the top of your PATH.

somehow, Python 2.6 is getting auto-aliased as my default python every time my shell starts

By auto-aliased, you mean there's a bash alias? As in you type alias and it gives you a list of things including python?

If so, you need to fix this, not try to pile another hack on top of it to undo whatever effect it has.

If there's no alias commands in ~/.*, look in /etc/. If you grep -r alias /etc, it will give you a big list of things, and you'll have to skip over the mail aliases and apache aliases (and possibly some permission denied output to stderr), but after that, there should be no shell aliases.

Further, my /usr/bin/python is not aliased to anything,

This makes me think you're confusing aliasing and symlinking. They're not the same thing. Which one is the problem? You have to know what's wrong before you can fix it.

So, try this:

which python

It should be /Library/Frameworks/Python.framework/Versions/2.7/bin/python if your PATH is what you say it is. If not, you're not setting up your PATH right, so echo $PATH and see.

If if is that, then ls -l /Library/Frameworks/Python.framework/Versions/2.7/bin/python. It should be a symlink to python2.7 or ./python2.7, not to anything in some other directory. If not, your installation is broken, and the best thing to do is uninstall Python and reinstall it properly. Or just don't reinstall it, and use Apple's. Or, if you actually need 2.7.3 instead of 2.7.2, or you need to build standalone py2app bundles (especially if you need them to be compatible with older OS X), or you're allergic to sudo and prefer having system-wide files being world-writable, or for some other reason you can't use Apple's, install from python.org or Homebrew instead of trying to do it yourself.

like image 78
abarnert Avatar answered Sep 21 '22 17:09

abarnert