Search code examples
python-3.xinstallationzlibbinascii

Zlib and binascii don't build with Python3.6


I've been trying to build Python3.6.1 from source code on Ubuntu 14.04. The sequence of commands is as recommended by README:

./configure
make
make test

The latter crashes because it cannot import binascii. In its output there is a following:

Following modules built successfully but were removed because they could not be imported:
binascii              zlib 

Trying to skip make test and start make install I have it crashing after failing to import zlib. Some folks in the Ubuntu forums suggested to update all the zlib's packages from repositories. That doesn't help. How do I fix this?


Solution

  • Try to install zlib from the source code(http://www.zlib.net/) manually (not via yum/apt-get/brew...) might be helpful.

    I have tried the Python3.6.1 building in my mac dev and also encountered your problem. It complains following message after making.

    Python build finished successfully!
    The necessary bits to build these optional modules were not found:
           ... zlib ...
    

    And I can't import zlib in interactive shell too.

    >>> import zlib
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: No module named 'zlib'
    

    I have solved the problem by the following steps.

    1. visit http://www.zlib.net/ and download zlib-1.2.11.

    2. install zlib (decompress, configure, make, make install).

    3. reinstall Python3.6.1 (make clean, make).

    I found the making process did not complain about zlib missing anymore and I could import zlib successfully in the shell.

    Actually, to solve this kind of problems, we might find some hints from the source code. We can find the following code in "setup.py" and the comments are pretty helpful. We can modify the code with debug information to locate where the problem really is (for me, it is because the first if check fails due to zlib.h missing).

        # You can upgrade zlib to version 1.1.4 yourself by going to
        # http://www.gzip.org/zlib/
        zlib_inc = find_file('zlib.h', [], inc_dirs)
        have_zlib = False
        if zlib_inc is not None:
            zlib_h = zlib_inc[0] + '/zlib.h'
            version = '"0.0.0"'
            version_req = '"1.1.3"'
            if host_platform == 'darwin' and is_macosx_sdk_path(zlib_h):
                zlib_h = os.path.join(macosx_sdk_root(), zlib_h[1:])
            with open(zlib_h) as fp:
                while 1:
                    line = fp.readline()
                    if not line:
                        break
                    if line.startswith('#define ZLIB_VERSION'):
                        version = line.split()[2]
                        break
            if version >= version_req:
                if (self.compiler.find_library_file(lib_dirs, 'z')):
                    if host_platform == "darwin":
                        zlib_extra_link_args = ('-Wl,-search_paths_first',)
                    else:
                        zlib_extra_link_args = ()
                    exts.append( Extension('zlib', ['zlibmodule.c'],
                                           libraries = ['z'],
                                           extra_link_args = zlib_extra_link_args))
                    have_zlib = True
                else:
                    missing.append('zlib')
            else:
                missing.append('zlib')
        else:
            missing.append('zlib')