Some notes on Python packaging stuff that wasn't obvious to me

August 23, 2014

A comment by Lars Kellogg-Stedman on this entry of mine wound up with me wanting to try out his lvcache utility, which is a Python program that's packaged with a Great, I thought, I know how to install these things.

Well, no, not any more. While I wasn't looking, Python packaging systems have gotten absurdly complex and annoying (and yes, one of the problems is that there are more than one of them). My attempts to install lvcache (either privately or eventually system-wide in a sacrificial virtual machine) failed in various ways. In the process they left me very frustrated because I had very little understanding of what a modern Python setup does when. Since I now have somewhat more understanding I'm going to write up what I know.

Once upon a time there was just site-packages with .py files and plain directories in it, and life was simple and good. If you wanted to you could augment the standard site-packages by setting $PYTHONPATH; the additional directories would be searched for .py files and plain directories too. Modern Python has added some wrinkles:

  • .pth files list additional paths that will be used for importing things from (generally relative to the directory you find them in). These additional import paths are visible in sys.path, so if you're not sure if a .pth file is working you can start Python and check what sys.path reports.

    .pth files in standard locations are loaded automatically; this includes your personal 'user' directory (on Unix, generally $HOME/.local/lib/pythonX.Y/site-packages, ie what 'python install --user' et al will use). However, .pth files in directories that are merely on your $PYTHONPATH are not automatically loaded by Python and must be bootstrapped somehow; if you use easy_install --prefix, it will stick a file to do this in the directory.

    (There are some really weird things that go on with .pth files. See Armin Ronacher.)

  • .egg files are ZIP files, which Python can import code from directly. They contain metadata and a module directory with .py files and normally appear directly on sys.path (eg the .egg file is listed itself). You can inspect .egg file contents with 'unzip -v thing.egg'. Under some circumstances it's possible for the install process to build a .egg that doesn't contain any Python code (or contains incomplete Python code); if you're facing mysterious failures, you may need to check for this.

  • .egg directories are unpacked versions of the ZIP versions above. I don't know when easy_install et al create directories versus files. Like the files they appear on sys.path directly. They can be inspected directly.

Modern installers no longer just put files and module directories in places. Instead, they make or obtain eggs and install the eggs. The good news is that things like easy_install follow dependencies (assuming that everyone has properly specified them, not always a given). The bad news is that this is much less inspectable than the old days.

(Okay, the other good news is that you can see which version of what you've installed by hand, instead of having a mess of stuff.)

In a properly functionally installed environment you should be able to fire up an interactive Python session and do 'import <module>' for every theoretically installed module. If this fails, either any .pth files are not getting bootstrapped (which can be checked by looking at sys.path), you don't have a module installed that you think you should, or perhaps the module is empty or damaged.

I'm sure all of this is documented in one or more places in the official Python documentation, but it is sure not easy to find if it is (and I really don't think there's one place that puts it all together).

PS: if you're installing a local copy of a package's source you want 'easy_install .' (in the source directory), likely with --user or --prefix. At least some of the time, easy_install will insist that you precreate the --prefix directory for it; it will always insist that you add it to $PYTHONPATH.

(The current anarchy of Python packaging and install systems requires another rant but I am too exhausted for it right now.)

Written on 23 August 2014.
« Where DTrace aggregates are handled for printing et al
My spam is (mostly) boring »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Sat Aug 23 00:32:11 2014
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.