2

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?

Synedraacus
  • 975
  • 1
  • 8
  • 21
  • 1
    Run the 3 commands and redirect each's _stdout_ (and _stderr_) to a file (e.g.: `./configure > configure_out.txt 2>&1`), and then make those 3 files accessible somewhere. Both those _Python_ modules rely on _zlib_: you might try (as _root_): `apt install zlib1g zlib1g-dev` (at least those are the pkg names on _Ubtu16 (x64)_) and then try building _Python_ again. – CristiFati Jul 11 '17 at 17:14

1 Answers1

1

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')
StringToken
  • 138
  • 7
  • I had the problem you're describing, solved it (`sudo apt-get install zlib-dev`, if my memory serves; I'm not at that computer right now) and *then* had the problem in the question. Although you're right, it might be helpful to check setup.py – Synedraacus Jul 16 '17 at 05:34
  • it seems installing zlib by apt-get or yum could not solve your problem (still encountering "ImportError: No module named 'zlib'" when "import zlib"). you have to install zlib from source code by hand [http://www.zlib.net/](http://www.zlib.net/). – StringToken Jul 16 '17 at 06:48
  • OK, I've accepted the answer, but please add the clarification re: manual installation to the answer itself. – Synedraacus Jul 17 '17 at 07:16