Fri 10 Feb 2006
All of my Python packages make use of setuptools, an extension to Python’s built-in distutils package that’s supposed to make life easier for everyone. After a recent mucking-about with my Python install, I got to see what happens if you try to use a setuptools-dependent package without having setuptools.
Now, you’d think the answer would be, “It doesn’t work. Duh,” but one of the package’s “features” is that
Your users don’t need to install or even know about setuptools in order to use them, and you don’t have to include the entire setuptools package in your distributions. By including just a single bootstrap module (an 8K .py file), your package will automatically download and install setuptools if the user is building your package from source and doesn’t have a suitable version already installed.
It’s that last bit that kills me. It’s just assumed that you have an always-on Internet connection, and God help the poor bastard who doesn’t.
Now, the logical thing to do would have been to have the bootstrap module attempt to download setuptools, but with a time out on the connection. If the module was unable to contact les Internets, it would attempt to fall back to the shipped-with-Python distutils. If the package-to-be-installed’s setup script used some setuptools-only feature, then it should inform the user that she’ll need to download setuptools manually.
My solution was to rip the bootstrap module out of my packages, then tweak setup.py like so: instead of
from setuptools import setup
I do this
try:
from setuptools import setup
except:
from distutils.core import setup
That way, if the user has setuptools installed, great, we’ll use that. If not, we fall back to distutils. This plan works, but it’s not foolproof; distutils still raises some harmless warnings about certain arguments to setup that it doesn’t recognise, but beyond that, this solution gets me where I want to go.
March 14th, 2006 at 07:02
Just an FYI, but you can bundle the setuptools .egg file along with the corresponding ez_setup.py in your source distribution, and no download will happen. The ez_setup autodownload feature is for the case where you don’t want to have to bundle the setuptools egg.