13

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/virtualenv.py", 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
zlib-1.2.3-29.el6.x86_64
zlib-1.2.3-29.el6.i686
zlib-devel-1.2.3-29.el6.x86_64

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.

Community
  • 1
  • 1
Pawel Miech
  • 7,742
  • 4
  • 36
  • 57

2 Answers2

13

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 setup.py 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.

Pawel Miech
  • 7,742
  • 4
  • 36
  • 57
l0b0
  • 55,365
  • 30
  • 138
  • 223
5

zlib is a Python module that interfaces with the zlib library on your computer. It is part of the standard library, so it should be on all Python 2.7 installs.

If it's not, in your case, then that likely means that your version of Python was compiled without zlib support for some reason. I can't imagine why, unless it's related to something you did to enable the use of older versions of zlib. I think you will need to find another Python package, or compile Python yourself.

Andrew Gorcester
  • 19,595
  • 7
  • 57
  • 73
  • Ok, so you suggest that it has to do with python27 installation. I will check it by reinstalling 2.7. – Pawel Miech Dec 29 '13 at 20:47
  • @Pawelmhm Actually, now that I look at it again, I may have been mistaken. I was confused between your two python environments. Python 2.6 appears to be the one that's being used to run virtualenv itself, so if you're able to import zlib in Python 2.6 that should be good enough. To assist in troubleshooting, can you try creating a Python 2.6 virtualenv (virtualenv with no additional arguments) and see if that works? – Andrew Gorcester Dec 29 '13 at 20:59
  • 1
    No, you were right, I just installed python2.7 again and it works. Apparently vitualenv requires zlib for created environment, not just for execution of virtualenv. I just had bad installation of python2.7 without zlib. – Pawel Miech Dec 29 '13 at 21:03
  • Virtualenv without --python argument worked fine before. I think I just installed python2.7 with make altinstall AND prefix argument to configure, this apparently installs stripped down version of python (without zlib). Anyway thanks very much! – Pawel Miech Dec 29 '13 at 21:06
  • @PawelMiech -- what command(s) did you use to uninstall or reinstall python2.7? Since yum is linked to python2.6, I'm trying to figure out the best way to remove my current version of python 2.7 and then reinstall it... – tandy Sep 20 '14 at 18:29
  • 1
    In my case I had compiled Python without the _zlib-devel_ package installed on my server. Installing _zlib-devel_ (and _openssl-devel_) with _yum_, then rebuilding Python fixed the problem. – nmgeek May 07 '15 at 17:00