Why install a third-party Python 2.7?

Python.org, Homebrew, scipy.org, 69105 different blog posts explaining how to install numpy/scipy/etc. on a Mac, etc. all tell you to install a different Python alongside Apple's. Almost nobody tells you to just use Apple's (except Homebrew, but they also tell you the opposite…).

There are some good reasons to install a third-party Python 2.7 on your Mac. If any of these apply, go install it:
  • You have an older OS X that doesn't come with 2.7.
  • You need one of the fixes in the latest 2.7.x.
  • You plan to use py2app to distribute binary applications. (As of 0.7.3, you can only build "semi-standalone" apps against 64-bit Apple Python.)
  • You don't have admin access, and for some reason can't install virtualenv into a user site-packages directory and do everything inside a venv.
  • You need an extra-batteries Python like Enthought.
  • You need the absolutely exact same Python on your 10.8 and 10.6 machines, or even (as far as ActiveState can make possible) on your Windows and Linux machines.
  • You use a non-usr-root package manager like MacPorts, Fink, or gentoo-alt.
  • You're hacking on the Python 2.x source.
But if none of those apply, do you really need another 2.7? Because there are an awful lot of downsides.

Why not install a third-party Python 2.7?

Parallel installs of the same minor version suck

Python is designed to handle parallel installs—but it does so by using X.Y directories and suffixes. So, if you have two 2.7s, you get:
  • Executable path collisions. Most Python installations will try to install extra scripts and executables like pip, ipython, etc. into /usr/local/bin. So, you'll end up silently replacing your python.org-Python ipython with an Apple-Python ipython. And "ipython-2.7" doesn't help, because they're both called that.
  • Executable name collisions. Even if they're in different directories, what gets run by "python2.7" or "pip-2.7" depends on which is higher on your path.
  • Site-packages collisions. There are a variety of site-packages directories, like /Library/Python/2.7, which will be shared by all of your 2.7 builds. That's usually fine for pure-Python code, but a C extension (like numpy or lxml) built for one Python 2.7 may insta-crash the other 2.7 if you're lucky, or cause instability or incorrect results if you're not.

$PATH Management is painful, even more so on Mac than other *nix

By default, /usr/local/bin comes later in the PATH than /usr/bin for shell scripts and sessions, and isn't on the path at all for GUI apps and LaunchAgents. So, when you type "python" or "python2.7", you'll still be getting Apple's Python.

You can change for your user scripts and sessions by munging your PATH in your .bash_profile or .profile, or system-wide in /etc/profile or /etc/paths, or for GUI apps and LaunchAgents in the appropriate environment.plist, etc. But you have to know which ones to change and why.

Also, other packages may expect /usr/local/bin to be after /usr/bin on the PATH.

There are plenty of people out there who can explain to you the difference between bash_profile and bashrc, bash_profile and profile, ~/.profile and /etc/profile, etc. But most of them are linux users who have no idea what an environment.plist is, how LaunchServices works, etc.—and they'll be mystified by the idea that anyone would have two system-wide installs of Python 2.7 in the first place.

Avoiding /usr/local helps, but not enough

The Python.org installers give you an option to leave /usr/local alone and install everything into the framework instead, letting you put /Library/Frameworks/Python.framework/Versions/2.7/bin on your PATH instead of /usr/local/bin.

Similarly, Homebrew lets you do a "keg-only" install, putting everything into /usr/local/Cellar/python/2.7.3/bin and not symlinking it into /usr/local/bin.

This solves a lot of the problems, but not all of them. You still have to get that path onto your PATH (and, when you forget what path was, you need to know how to find it again). And you still have name collisions, so you can still end up with "pip-2.7" installing packages that "ipython2.7" can't find.

Weird Apple libraries break stuff

Apple often installs specially-patched, bleeding-edge, or otherwise weird libraries, which they may test with their own Python, but not with third-party Pythons. This can cause problems like:
  • On Snow Leopard 10.6, Apple's Tcl/Tk works with their Python, but with python.org's, IDLE and tkinter are broken. You have to follow the directions on python.org to install ActiveTcl.
  • On Lion 10.7, Apple's libxml2 works with lxml built against their Python, but not against Homebrew Python. You have to follow the directions on the Homebrew wiki to install a keg-only libxml2 package, and figure out for yourself how to get lxml built against it.

Apple extras are missing

Apple's Python comes with a few extra batteries, notably PyObjC. Besides the fact that PyObjC is a huge package that can be a pain to install (especially if you're an early adopter of a new OS X version), there are scripts out there that expect to be able to just "import AppKit" or "import AddressBook". And when you asking someone why "import AddressBook" doesn't work, 99% of the time they'll go looking for a package with that name on PyPI, or tell you that you need to read the prerequisites for whatever package you're trying to use, which is not helpful.

Community support is unhelpful

There's a lot of great help out there for Python, from the official mailing list to sites like StackOverflow and SuperUser.

Almost none of these people can help you with running two system Python 2.7 versions in parallel. Linux and Windows users don't often do that. Many Mac users do, but only because they followed the same blog as you without really understanding what they were doing, and all they can say is, "Well, it worked for me."

Within the scipy community, and possibly certain other niches, things are different. There are a lot of Mac users, many of whom regularly do things like hacking on C extensions, and they all use python.org 2.7. If you plan to ask for more help among that community than in the general Python world, you may be better off using python.org 2.7. (But even there, honestly, I'd try it with Apple Python first, and only install another 2.7 if you run into problems. See Installing scipy, etc., with Apple's Python 2.7 and there's a good chance it will just work.)

So, why does everyone say to not use Apple's Python?

First, it's a whole lot easier to just say "Install the python.org version" than "If you don't have an Apple version 2.7, or you have an Apple 2.7.2 version but need the 2.7.3 fixes, or…"

Historically, Apple used to distribute very outdated, incomplete, and/or broken Python versions. The conventional wisdom was built in the pre-Leopard era where using Apple's Python was a fool's errand. Most of the people who really know what they were doing got things working in Apple's earlier systems, and haven't had any reason to learn why Apple's newer systems are different.

And even among those who know that Apple's doing things better, some don't trust Apple. How do we know OS X 10.9 won't come with Python 3.1 installed as python3? Or that 10.8.5 won't suddenly drop some of the key development files for 2.7, so you can't build C extensions anymore (as they did at one point in the old days)?

Finally, the people involving in packaging up Python for python.org are much more a part of the public community than the people involved in packaging it up as part of the OS X install.

So why should I listen to you?

I see dozens of people on StackOverflow, mailing lists, in real life, etc. who don't understand how to manage two Python 2.7 installations, and are only doing it because "everyone says to do it", and can't get it to work.

Even if you know what you're doing, using Apple's Python is just much, much easier. While I set up a build machine with python.org 2.7.3 to run nightly tests and build py2app packages and so on, my personal dev boxes use Apple's 2.7.2, because it saves me a bit of time in setting up a new dev box, and a whole lot of time in avoiding regular hassles.

So, I think the blanket statement that most people should install another Python is wrong, at least in the 10.8 era. Certainly some people should. If you already know you need 2.7.3 or a py2app-capable standalone install or whatever, or you started with Apple's Python and then ran into a brick-wall problem with it, or your first source for help is the physics grad students down the hall rather than a site like StackOverflow, don't hesitate to install another 2.7. But otherwise, you may be a lot happier sticking with Apple's.

Shouldn't I just use 3.3 instead of 2.7 anyway?

Yes! There are plenty of good reasons some people need to stay on 2.x (see Python 2 or Python 3 on the official site for details—although it's more than 2 years out of date—and check PyPI to see if all the packages you need are 3.x-compatible). But if none of them apply to you, "Python 3.x is the present and the future of the language."

Plus, Python is designed to have multiple installations in parallel as long as they're not the same minor version, so most of the downsides in this post won't matter.

So, if you can use 3.3 instead of 2.7, go get it from python.org and stop worrying.

What about PyPy, IronPython, or Jython?

Well, for IronPython or Jython, if you need it, you probably already know you need it.

For PyPy, it's usually a tradeoff of "runs my code faster" vs. "doesn't have support for many libraries that I need". (As with 3.x, the situation is getting better every day, but may not be good enough yet for you.) And, even if performance or other features aren't important to you, it's a solid Python 2.7.3 implementation that can be installed in parallel with CPython 2.7.2 and happens to be faster to boot. But really, in most cases I can think of where you need 2.7 instead of 3.x, and can't use Apple's 2.7, you also can't use PyPy.

Still, if you can, go for it.
0

Add a comment

It's been more than a decade since Typical Programmer Greg Jorgensen taught the word about Abject-Oriented Programming.

Much of what he said still applies, but other things have changed. Languages in the Abject-Oriented space have been borrowing ideas from another paradigm entirely—and then everyone realized that languages like Python, Ruby, and JavaScript had been doing it for years and just hadn't noticed (because these languages do not require you to declare what you're doing, or even to know what you're doing). Meanwhile, new hybrid languages borrow freely from both paradigms.

This other paradigm—which is actually older, but was largely constrained to university basements until recent years—is called Functional Addiction.

A Functional Addict is someone who regularly gets higher-order—sometimes they may even exhibit dependent types—but still manages to retain a job.

Retaining a job is of course the goal of all programming. This is why some of these new hybrid languages, like Rust, check all borrowing, from both paradigms, so extensively that you can make regular progress for months without ever successfully compiling your code, and your managers will appreciate that progress. After all, once it does compile, it will definitely work.

Closures

It's long been known that Closures are dual to Encapsulation.

As Abject-Oriented Programming explained, Encapsulation involves making all of your variables public, and ideally global, to let the rest of the code decide what should and shouldn't be private.

Closures, by contrast, are a way of referring to variables from outer scopes. And there is no scope more outer than global.

Immutability

One of the reasons Functional Addiction has become popular in recent years is that to truly take advantage of multi-core systems, you need immutable data, sometimes also called persistent data.

Instead of mutating a function to fix a bug, you should always make a new copy of that function. For example:

function getCustName(custID)
{
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

When you discover that you actually wanted fields 2 and 3 rather than 1 and 2, it might be tempting to mutate the state of this function. But doing so is dangerous. The right answer is to make a copy, and then try to remember to use the copy instead of the original:

function getCustName(custID)
{
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

function getCustName2(custID)
{
    custRec = readFromDB("customer", custID);
    fullname = custRec[2] + ' ' + custRec[3];
    return fullname;
}

This means anyone still using the original function can continue to reference the old code, but as soon as it's no longer needed, it will be automatically garbage collected. (Automatic garbage collection isn't free, but it can be outsourced cheaply.)

Higher-Order Functions

In traditional Abject-Oriented Programming, you are required to give each function a name. But over time, the name of the function may drift away from what it actually does, making it as misleading as comments. Experience has shown that people will only keep once copy of their information up to date, and the CHANGES.TXT file is the right place for that.

Higher-Order Functions can solve this problem:

function []Functions = [
    lambda(custID) {
        custRec = readFromDB("customer", custID);
        fullname = custRec[1] + ' ' + custRec[2];
        return fullname;
    },
    lambda(custID) {
        custRec = readFromDB("customer", custID);
        fullname = custRec[2] + ' ' + custRec[3];
        return fullname;
    },
]

Now you can refer to this functions by order, so there's no need for names.

Parametric Polymorphism

Traditional languages offer Abject-Oriented Polymorphism and Ad-Hoc Polymorphism (also known as Overloading), but better languages also offer Parametric Polymorphism.

The key to Parametric Polymorphism is that the type of the output can be determined from the type of the inputs via Algebra. For example:

function getCustData(custId, x)
{
    if (x == int(x)) {
        custRec = readFromDB("customer", custId);
        fullname = custRec[1] + ' ' + custRec[2];
        return int(fullname);
    } else if (x.real == 0) {
        custRec = readFromDB("customer", custId);
        fullname = custRec[1] + ' ' + custRec[2];
        return double(fullname);
    } else {
        custRec = readFromDB("customer", custId);
        fullname = custRec[1] + ' ' + custRec[2];
        return complex(fullname);
    }
}

Notice that we've called the variable x. This is how you know you're using Algebraic Data Types. The names y, z, and sometimes w are also Algebraic.

Type Inference

Languages that enable Functional Addiction often feature Type Inference. This means that the compiler can infer your typing without you having to be explicit:


function getCustName(custID)
{
    // WARNING: Make sure the DB is locked here or
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

We didn't specify what will happen if the DB is not locked. And that's fine, because the compiler will figure it out and insert code that corrupts the data, without us needing to tell it to!

By contrast, most Abject-Oriented languages are either nominally typed—meaning that you give names to all of your types instead of meanings—or dynamically typed—meaning that your variables are all unique individuals that can accomplish anything if they try.

Memoization

Memoization means caching the results of a function call:

function getCustName(custID)
{
    if (custID == 3) { return "John Smith"; }
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

Non-Strictness

Non-Strictness is often confused with Laziness, but in fact Laziness is just one kind of Non-Strictness. Here's an example that compares two different forms of Non-Strictness:

/****************************************
*
* TO DO:
*
* get tax rate for the customer state
* eventually from some table
*
****************************************/
// function lazyTaxRate(custId) {}

function callByNameTextRate(custId)
{
    /****************************************
    *
    * TO DO:
    *
    * get tax rate for the customer state
    * eventually from some table
    *
    ****************************************/
}

Both are Non-Strict, but the second one forces the compiler to actually compile the function just so we can Call it By Name. This causes code bloat. The Lazy version will be smaller and faster. Plus, Lazy programming allows us to create infinite recursion without making the program hang:

/****************************************
*
* TO DO:
*
* get tax rate for the customer state
* eventually from some table
*
****************************************/
// function lazyTaxRateRecursive(custId) { lazyTaxRateRecursive(custId); }

Laziness is often combined with Memoization:

function getCustName(custID)
{
    // if (custID == 3) { return "John Smith"; }
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

Outside the world of Functional Addicts, this same technique is often called Test-Driven Development. If enough tests can be embedded in the code to achieve 100% coverage, or at least a decent amount, your code is guaranteed to be safe. But because the tests are not compiled and executed in the normal run, or indeed ever, they don't affect performance or correctness.

Conclusion

Many people claim that the days of Abject-Oriented Programming are over. But this is pure hype. Functional Addiction and Abject Orientation are not actually at odds with each other, but instead complement each other.
5

View comments

Blog Archive
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.