building Python from source with zlib support

后端 未结 10 769
广开言路
广开言路 2020-11-28 04:18

When building Python 3.2.3 from source on Ubuntu 12.04, the zlib module is not available.

I downloaded the official source distribution from python.org, and attempte

10条回答
  •  野性不改
    2020-11-28 04:52

    I was having the same error while working on MAC

    My MAC OS version

    $ uname -v
    Darwin Kernel Version 13.4.0: Sun Aug 17 19:50:11 PDT 2014; root:xnu-2422.115.4~1/RELEASE_X86_64
    

    python3.4 is used here

    Issue(s)

    1. zlib not available while using python3.4

      $ python3.4 get-pip.py Traceback (most recent call last): File "get-pip.py", line 20204, in main() File "get-pip.py", line 152, in main bootstrap(tmpdir=tmpdir) File "get-pip.py", line 82, in bootstrap import pip zipimport.ZipImportError: can't decompress data; zlib not available

    2. Rebuilding Python fails

      ./configure --with-zlib-dir=/usr/local/lib

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

    Solution

    1. Ensure zlib is installed . By default it will be installed in /usr/lib

      ls /usr/lib/libz.*

    If not installed, a. download and install i)from zlib.net site or ii) from a git repo like the below

    git clone https://github.com/madler/zlib.git 
    

    or iii). Use the zlib source in the python source directory Modules/zlib

    b. Install zlib

    ./configure --prefix=/usr/local
    make
    sudo make install 
    

    2.Edit /Module/Setup by uncommenting the line below "#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz "

    3.Rebuild the Python3.4 from source again

    cd ${PYTHON_SRC_CODE_DIR}  
    ./configure --prefix=${PYTHON_HOME_DIR}
    make
    sudo make install 
    

    4.Confirm installation Please note gzip depends on zlib.

    nbr_repeation=100
    f=open("some_file.txt","at")
    for line in range(nbr_repeation): 
        print('[{}] This file will be compressed using python zlib/gzipmodule'.format(line),file=f)
    
    f.close()
    f=open("some_file.txt","rt")
    import gzip
    gz=gzip.open('some_file.gz', 'wt') 
    for line in f : gz.write(line)
    
    gz.close() # Like to be clean exit
    f.close()  # Like a clean exit
    
    """confirm the creation of the compressed gzip files"""
    import os
    print([ (file,os.stat(file)[6],"bytes") for file in os.listdir(".") if file.startswith("some")])
    

提交回复
热议问题