Python – Building Python 3.3 with LZMA on Linux and OS X

Python 3.3 supports LZMA compression, though you’ll need an LZMA implementation installed in order to use it.

New in Python 3.3, the lzma module provides support for .lzma and .xz compressed files and streams.

In order for it to work though you’ll need an LZMA implementation on your system. If you don’t have one Python will happily build, though make will warn you it’s missing:-

Python build finished, but the necessary bits to build these modules were not found:
_lzma

To find the necessary bits, look in setup.py in detect_modules() for the module's name.

Also, if you care to run it, make test will warn you of the unexpected skip:-

1 skip unexpected on darwin:
    test_lzma

This means you either have no LZMA library installed or the Python build can’t see one. You can check – whatever your Unix flavour – by running a find for lzma.h, the header file required by the Python build.

find /usr /opt -name "lzma.h" -print

If this comes back with no matches, you’ll need to install an LZMA package on your system. If it does come back with a match, you’ll need to point the Python build to it.

Don’t have it?

LZMA comes as part of the XZ open source compression library. One way to get the required files onto your system is to download the latest source bundle from http://tukaani.org/xz/ and follow the normal configure -> make -> make install build cycle.

On OS X the MacPorts project provides a binary XZ package that will give you what you need. If you don’t already have MacPorts installed, first follow the instructions on http://www.macports.org/install.php to get it.

Run the following command to add the XZ package to your Mac:-

sudo /opt/local/bin/port install xz

This won’t install it where the Python build expects it though, so you’ll need to check out the Can’t see it section below before running your Python build again.

If your Linux distribution has yum you can use the following command to install the XZ package and the associated development files needed for Python to build with it:-.

yum install xz-devel

Can’t see it?

If you have the LZMA headers and libraries installed but Python’s make still doesn’t build with it the most likely problem is that the headers and libraries aren’t where the Python build is expecting them to be. This will be the case on OS X with the MacPorts package which will install to /opt/local whereas the Python build will be looking under /usr.

You’ll need to set the CFLAGS environment variable to point to the directory containing lzma.h, and the LFLAGS environment variable to point to the directory containing the liblzma files, then run configure and make again:-

export LDFLAGS="-L/opt/local/lib"
export CFLAGS="-I/opt/local/include"
./configure
make clean
make

If your make successfully builds the _lzma module, but make test still reports it as a skip, it may be that the directory containing the liblzma shared library (e.g. liblzma.so.5) needs to be added to your shared library path. For example on Red Hat Linux 5.6 with XZ built from source, make test reports:-

*** WARNING: renaming "_lzma" since importing it failed: liblzma.so.5: cannot open shared object file: No such file or directory

Setting LD_LIBRARY_PATH to include the folder containing the shared object gets rid of this error.

export LD_LIBRARY_PATH=/usr/local/lib
make test

Leave a Reply

Your email address will not be published. Required fields are marked *