Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

building Python from source with zlib support

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 attempted to build and install it with the following commands.

tar xfa Python3.2.3.tar.bz2
cd Python-3.2.3
./configure --prefix=/opt/python3.2
make
sudo make install

The make command output includes the following.

Python build finished, but the necessary bits to build these modules were not found:
_curses            _curses_panel      _dbm            
_gdbm              _sqlite3           _ssl            
_tkinter           bz2                readline        
zlib                                            

After running make install and starting the interpreter, the zlib module cannot be imported.

I confirmed that the zlib1g-dev package is installed on my system.

I also found this similar question, which suggests adding the --with-zlib flag to the ./configure command. However, that returns an error that it is an unrecognized option and has no effect.

like image 414
joshayers Avatar asked Sep 10 '12 03:09

joshayers


People also ask

How do I download a Python source code?

Point your browser at https://pypi.org/project/<packagename> Select either Download Files to download the current package version, or Release History to select the version of your choice. Click on the package to save it to a location on your computer or network.

How do I install Python source code in Windows?

To install Python from source code, you need a platform with an ISO-compliant C compiler and ancillary tools such as make. On Windows, the normal way to build Python is with the Microsoft product Visual C++. To download Python source code, visit http://www.python.org and follow the link labeled Download.


9 Answers

I had a similar problem on CentOS 6.3 and python 3.2.3

I solved it by:

Edit /Modules/Setup and uncomment the line:

zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

change to directory /Modules/zlib:

./configure
make
sudo make install

then compiled my python3.2 source.

and was then able to test import zlib and it all worked fine :)

like image 108
asanadi Avatar answered Oct 05 '22 11:10

asanadi


I am using CentOS 6.6 and was recieving zlib errors. None of the other answers proposed here worked for me (including the fix for CentOS 6.3 of uncommenting a line in Modules/Setup). I have fixed it using the following commands.

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

Then configuring and installing python as follows:

./configure --prefix=/usr/local LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall

I can now import zlib in /usr/local/bin/python2.7 with no problems.

These instructions are slightly modified from an article found here.

like image 43
jhrf Avatar answered Oct 05 '22 11:10

jhrf


The solution is to install the Ubuntu package dpkg-dev.

sudo apt-get install dpkg-dev

The reason is explained here.

In short, recent versions of Ubuntu don't store libz.so in the standard /usr/lib location, but rather in a platform specific location. For example, on my system is is in /usr/lib/x86_64-linux-gnu. This prevents Python's build system from finding it.

The dpkg-dev package installs the dpkg-architecture executable, which enables Python to find the necessary libraries.

The original question was about Python 3.2.3. I also downloaded Python 2.7.3 and confirmed that the same problem exists, and this solution is applicable to it as well.

like image 20
joshayers Avatar answered Oct 05 '22 10:10

joshayers


For anyone who's trying to use a non-system / non-standard zlib (e.g. building your own from source), make sure to pass both CPPFLAGS (not CFLAGS!) and LDFLAGS to ./configure. For example, if your zlib is in /opt/zlib:

./configure CPPFLAGS='-I/opt/zlib/include' LDFLAGS='-L/opt/zlib/lib'
make
sudo make install

I ended up going down the rabbit hole trying to figure out why our Python wasn't building with zlib support and found out that the CPython setup.py does not look at CFLAGS for include dirs, only CPPFLAGS: https://github.com/python/cpython/blob/master/setup.py#L562

like image 38
isobit Avatar answered Oct 05 '22 11:10

isobit


The only solution that helped me with installing python 3.5.1 was to apt-get zlib1g-dev (and other packages such as python-setuptools and python-pip) and then rebuild python 3.5.1 from source.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus
sudo apt-get install build-essential libncursesw5-dev libgdbm-dev libc6-dev
sudo apt-get install zlib1g-dev libsqlite3-dev tk-dev
sudo apt-get install libssl-dev openssl
cd ~
mkdir build
cd build
wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
tar -zxvf Python-3.5.1.tgz
cd Python-3.5.1
./configure
make
sudo make install

Taken from: https://github.com/MrYsLab/xideco/wiki/Installing-Python-3.5

As I undestand new build of python is made with inclusion of previously apt-getted related packages. So when you browse the content of new Python-3.5.1/lib/site-packages there will be pip and setuptools. More importantly, they will be copied to any virtualenv you make using Python-3.5.1 AND this virtualenv will use THEM insted of system-default. This is very, very important to rememmber when installing new python version. Otherwise one might get into a black hole of errors such as:

  • zlib not installed;
  • "pip install ..." executed from virtualenv that installs package to system-default python instead of virtualenv.
like image 32
Kshatra Avatar answered Oct 05 '22 11:10

Kshatra


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")])
like image 43
Adebiyi Abdurrahman Avatar answered Oct 05 '22 11:10

Adebiyi Abdurrahman


sudo apt-get install build-essential python-dev

Even though python-dev is for python2.7 it will still bring in all the necessary dependencies.

You will then need to do:

./configure
make
sudo make install

To rebuild python3

like image 20
freshnewpage Avatar answered Oct 05 '22 10:10

freshnewpage


The easiest solution I found, is on python.org:

sudo apt-get build-dep python3.6

If that package is not available for your system, try reducing the minor version until you find a package that is available in your system’s package manager.

If you see something like this: E: You must put some ‘source’ URIs in your sources.list, Open Software & Updates and enable Source code.

I tried explaining details, on a blog post.

like image 22
shibli049 Avatar answered Oct 05 '22 10:10

shibli049


sudo apt-get install zlib1g-dev

is what worked for me.

like image 21
Hazzles Avatar answered Oct 05 '22 12:10

Hazzles