28 February 2012

Python virtualenv, no zlib module was found

This is my story when for the first time using virtualenv in Python. My installed Python on Linux box was using 2.7 version, on the other side my current project is still using 2.6. Replacing 2.7 with 2.6 in my OS ? No, it's bad option. Virtualenv is the best way.

The problem occurred when I try to use Python 2.6 for my virtualenv using command below:

sudo virtualenv -p /usr/local/bin/python2.6

sadly it produce error:

Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/", line 17, in
import zlib

ImportError: No module named zlib

I try to use 2.7 rather than 2.6, it success, no error produced. So the problem is about my installed Python 2.6 thought.

Try to looking for information about this problem, finally I found. Someone said that that error is because there is no zlib installed on my Pyhton 2.6 when I install it.

By default when we make configure on Python source, zlib module is disabled. So we have to enable it explicitly. using

So the configuration command becomes ./configure --with-zlib

now try
sudo virtualenv -p /usr/local/bin/python2.6

And it successes. :)


  1. When I ran the configure I got:

    configure: WARNING: unrecognized options: --with-zlib

    and the make and make install resulted in a python without zlib

  2. First, you have to enable it by uncomment zlib in Modules/Setup in the Python source folder.

    And then try install Python with zlib option enable:

    ./configure --with-zlib

    If it's fail, make sure you have zlib1g-dev installed.

    *You can also give prefix to zlib (./configure --with-zlib=/your/path) but this is not a must.