32

It seems that Python 2.6.1 doesn't compile bz2 library by default from source.

I don't have lib-dynload/bz2.so

What's the quickest way to add it (without installing Python from scratch)?

OS is Linux 2.4.32-grsec+f6b+gr217+nfs+a32+fuse23+tg+++opt+c8+gr2b-v6.194 #1 SMP Tue Jun 6 15:52:09 PDT 2006 i686 GNU/Linux

IIRC I used only --prefix flag.

Piotr Byzia
  • 3,363
  • 7
  • 42
  • 62
  • possible duplicate of [ImportError: No module named bz2 for Python 2.7.2](http://stackoverflow.com/questions/8115280/importerror-no-module-named-bz2-for-python-2-7-2) – Abizern Aug 13 '12 at 08:53

4 Answers4

35

You need libbz2.so (the general purpose libbz2 library) properly installed first, for Python to be able to build its own interface to it. That would typically be from a package in your Linux distro likely to have "libbz2" and "dev" in the package name.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
30

Use your vendor's package management to add the package that contains the development files for bz2. It's usually a package called "libbz2-dev". E.g. on Ubuntu

sudo apt-get install libbz2-dev

user95975
  • 466
  • 3
  • 3
20

There are 2 solutions for this trouble:

option 1. install bzip2-devel

On Debian and derivatives, you can install easily like this:

sudo apt-get install bzip2-devel

option 2. build and install bzip2

In the README file of bzip2 package, it is explained that under certain platforms, namely those which employ Linux-ELF binaries, you have to build an additional shared object file like shown below:

wget http://bzip.org/1.0.6/bzip2-1.0.6.tar.gz
tar xpzf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6
make
make -f Makefile-libbz2_so
make install PREFIX=/path/to/local # /usr/local by default

The critical bit here is the following command:

make -f Makefile-libbz2_so

I've done this and after that tried to build Python again, like shown below:

cd Python-2.7.3
./configure --prefix=/path/to/local 
make install
mspisars
  • 844
  • 8
  • 21
Richard Gomes
  • 5,675
  • 2
  • 44
  • 50
  • The readme says make -f Makefile-libbz2_so instead of make -f Makefile_libbz2_so – mr2ert Jun 28 '13 at 23:10
  • Unable to locate package bzip2-devel (squeeze) – chovy Aug 13 '13 at 07:41
  • @chovy: "apt-cache search bzip | fgrep dev". You can try "libbz2-dev" – Richard Gomes Aug 24 '13 at 14:22
  • Did the build Python succeed? – Mikko Koho Feb 23 '15 at 20:26
  • When I try compiling this way, I get an error that says "recompile with -fPIC". When I do `make -fPIC Makefile-libbz2_so`, I get `make: *** No rule to make target \`PIC'. Stop.` – hlin117 Apr 01 '15 at 00:51
  • 9
    There is a bug here, if you have run `make`, it is necessary to run `make clean` before attempting `make -f Makefile-libbz2_so`. – shuckc Aug 24 '15 at 17:08
  • 2
    @hlin117 `-fPIC` is a flag for the compiler, not an option for `make`. You have to pass it as an environment variable as follows: `make -f Makefile-libbz2_so CFLAGS="-fPIC"` – bli Feb 08 '17 at 14:34
10

If you happen to be trying to compile Python on RHEL5 the package is called bzip2-devel, and if you have RHN set up it can be installed with this command:

yum install bzip2-devel

Once that is done, you don't need either of the --enable-bz2 or --with-bz2 options, but you might need --enable-shared.

Eric
  • 5,137
  • 4
  • 34
  • 31