Pawel Miech Pawel Miech - 4 years ago 332
Python Question

Virtualenv no module named zlib

I'm trying to create Python 2.7 virtual env under Python2.6, I'm simply running:

virtualenv --python=python27 #python27 correctly leads to my python installation in /opt/python2.7/bin/python

Virtualenv fails with following error

Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/", line 17, in <module>
import zlib
ImportError: No module named zlib

This puzzles me because:

1) I clearly do have python zlib module. I can import it easily when I run "import zlib" in interactive environment of python 2.6.

2) I also have zlib installed in my system (centos):

[me@mycomp]# rpm -qa | grep zlib

There are two other questions concerning this issue, here and here, in both cases people simply don't have zlib installed which is not my case (I have it in python 2.6, which should be ok right?), they are also using pythonbrew which is apparently no longer under active development.

Why virtualenv can't find zlib? How virtualenv looks up its modules? Do I need to install zlib in my Python2.7? Or reinstall Python2.7 so that zlib is suppported?

Sidenote:please don't ask why I'm using old versions, it's not my choice.

Answer Source

Your Python must have been compiled without Python support, most likely because zlib-devel was not installed when it was compiled. Looking at the output of make or make install you should see something like the following (taken from a build of Python 2.7.6):

Python build finished, but the necessary bits to build these modules were not found:
[...]        zlib            
To find the necessary bits, look in in detect_modules() for the module's name.

To solve your problem you need to install zlib-devel (if it is not installed) and recompile or reinstall Python.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download