Building Python 3.7.1 - SSL module failed - python

Building Python 3.7 from source runs into following error:
Failed to build these modules:
_hashlib _ssl
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
I tried so many workarounds from other stackoverflow-questions, but it doesnt work. I build newest OpenSSL and LibreSSL from source. OpenSSL path is: "/usr/local/ssl" with version OpenSSL 1.0.2p.
./configure --with-openssl=/usr/local/ssl/
(./configure CPPFLAGS="-I/usr/local/ssl/include" LDFLAGS="-L/usr/local/ssl/lib")
make
make altinstall
My system:
Ubuntu 12.04.5 LTS
Any ideas?

I solved it after 3 days only because of this blog. with python 3.7.4 openssl 1.1.0 centOS 6.
here is the summary :
First, some prerequisites:
sudo apt-get install build-essential checkinstall libreadline-gplv2-dev libncursesw5-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
use yum instead of apt-get if using centos linux.
Install ssl 1.0.2 or higher.
cd /usr/src
curl https://www.openssl.org/source/openssl-1.0.2o.tar.gz | tar xz
cd openssl-1.0.2o
./config shared --prefix=/usr/local/
sudo make
sudo make install
We will need to pass /usr/src/openssl-1.0.2o into the Python configure script.
mkdir lib
cp ./*.{so,so.1.0.0,a,pc} ./lib
Now proceed with installing Python:
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
sudo tar xzf Python-3.7.0.tgz
cd Python-3.7.0
./configure --with-openssl=/usr/src/openssl-1.0.2o --enable-optimizations
sudo make
sudo make altinstall
To test it out, run python3.7 and input:
import ssl
ssl.OPENSSL_VERSION
Hope it helps!

Compiling openssl
Download your openssl tarball, unzip, and then ensure that the install directory is named openssl.
I placed mine in /usr/local/openssl, so I'll use that in my example.
sudo mv openssl-1.0.2u /usr/local/openssl && cd /usr/local/openssl
sudo make distclean
sudo ./config -fPIC -shared
sudo make && sudo install
Now, add the openssl shared library to your PATH.
vim ~/.profile
Go
export LD_LIBRARY_PATH="/usr/local/openssl/lib:$LD_LIBRARY_PATH"
:wq
Compiling Python3
The key here is understanding that the path you define with --with-openssl= is where Python looks for /openssl/lib. You need to give Python the parent directory of the openssl directory.
That means that if you set --with-openssl=/usr/local/openssl your make install will fail even though the make logs show that openssl is fine!
--enable-optimizations is irrelevant but recommended - longer make for 10% faster Python code is a good tradeoff.
--prefix= is merely where I'd like python3 to install, if you didn't know.
sudo make distclean
Edit your python setup file
vim /{yourpythonsource}/Modules/Setup
Uncomment out the following lines and ensure that your SSL variable points to your openssl directory. In mine, it was looking for the directory 'ssl' instead of 'openssl.'
<pre><code># Socket module helper for SSL support; you must comment out the other </code>
<pre><code># socket line above, and possibly edit the SSL variable: </code>
<code>SSL=/usr/local/openssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto</code>
sudo ./configure --with-openssl=/usr/local --prefix=/opt/python-3.7.1
sudo make && sudo make install

While this might not be the best answer, I will share how I solved this problem.
First of all, in my case, OpenSSL did not build correctly, as make test did return errors (and consequently Python gave this error). This was solved by installing a newer version of Perl and then installing OpenSSL again (configure, make, etc).
Use this command before using ./configure
export LD_LIBRARY_PATH=/path/to/openssl/lib:$LD_LIBRARY_PATH
At the configure command, include the library:
LDFLAGS="-L/path/to/openssl/lib" ./configure (all your preferred options) --with-openssl=/path/to/openssl
as apparently the option for configure does not convey the message to the C compiler which needs it.
Am not sure whether option 2 and 3 are needed simultaneously, but I did so and it worked.

Edit setup.py
Find the following lines:
system_lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib']
system_include_dirs = ['/usr/include']
...and place each folder at the beginning of its respective list.
In my case I had to add: /usr/local/lib and /usr/local/include:
system_lib_dirs = ['/usr/local/lib', '/lib64', '/usr/lib64', '/lib', '/usr/lib']
system_include_dirs = ['/usr/local/include', '/usr/include']
Finally: make distclean && ./configure
You may want to ensure that export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH (or what have you) is added to the very end of /etc/profile and reboot, as well.

On CentOS / Linux 2 if you install openssl using
sudo yum install -y openssl-devel
then the library is installed to /usr/local/lib64, and you can configure Python as follows:
./configure --enable-shared --with-openssl=/usr/local/lib64
there are step-by-step instructions here: How to Install Latest (2020) Django to AWS EC2 Linux 2 Instance and Serve w/ Apache Hello World

I ran into this problem with LMDE 5 (running Debian Bullseye) compiling Python 3.10.4. It was fixed by doing:
sudo apt-get install libssl-dev

There was NO need to edit Modules/Setup file built python with customed openssl.
I have built python 3.11.0-rc2 under Debian 9 stretch follow the official document:
https://docs.python.org/3/using/unix.html?highlight=openssl#custom-openssl
To use your vendor’s OpenSSL configuration and system trust store, locate the directory with openssl.cnf file or symlink in /etc. On most distribution the file is either in /etc/ssl or /etc/pki/tls. The directory should also contain a cert.pem file and/or a certs directory.
$ find /etc/ -name openssl.cnf -printf "%h\n"
/etc/ssl
Download, build, and install OpenSSL. Make sure you use install_sw and NOT install. The install_sw target does NOT override openssl.cnf.
$ curl -O https://www.openssl.org/source/openssl-VERSION.tar.gz
$ tar xzf openssl-VERSION
$ pushd openssl-VERSION
$ ./config \
--prefix=/usr/local/custom-openssl \
--libdir=lib \
--openssldir=/etc/ssl
$ make -j1 depend
$ make -j8
$ make install_sw
$ popd
Build Python with custom OpenSSL (see the configure --with-openssl and --with-openssl-rpath options)
$ pushd python-3.x.x
$ ./configure -C \
--with-openssl=/usr/local/custom-openssl \
--with-openssl-rpath=auto \
--prefix=/usr/local/python-3.x.x
$ make -j8
$ make altinstall
ssl module check after installed :
# /usr/local/python-3.11.0-rc2/bin/python3.11 -c 'import ssl; print(ssl.OPENSSL_VERSION)'
OpenSSL 1.1.1q 5 Jul 2022

Met same issue, looks configure of Python3 can't work well.
If you have installed the latest openssl, make sure the path of OPENSSL_LDFLAGS is correct in Makefile, below is my env case
OPENSSL_LDFLAGS=-L/usr/local/lib64

Execute till download python (3.10.4 is what i tried) from the link below
https://computingforgeeks.com/install-latest-python-on-centos-linux/
Upgrade openssl as documented in https://cloudwafer.com/blog/installing-openssl-on-centos-7/
modify $python_home/Modules/Setup
Update the OPENSSL location and uncomment the below lines
--------------------------------------------
OPENSSL=/usr/local/ssl
_ssl _ssl.c \
-I$(OPENSSL)/include -L$(OPENSSL)/lib \
-lssl -lcrypto
--------------------------------------------
Continue the installation steps from https://computingforgeeks.com/install-latest-python-on-centos-linux/
Hope it helps somebody..
fyi: I was installing this on a centos7 ec2 instance as a part of installing ansible.

Here is a solution on Mac OS X / Homebrew:
brew reinstall openssl
brew unlink openssl && brew link openssl --force # careful!
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
Then download your python tarball and do this:
tar xvf Python-3.7.2.tar
cd Python-3.7.2
./configure CPPFLAGS="-I/usr/local/opt/openssl/include" LDFLAGS="-L/usr/local/opt/openssl/lib" --prefix=$PWD/Python-3.7.2/mybuild --enable-optimizations
More detai:
https://devguide.python.org/setup/#macos-and-os-x

Related

Building Python and OpenSSL from source, but ssl module fails

I'm trying to build Python and OpenSSL from source in a container. Both seem to build correctly, but Python does not successfully create the _ssl module.
I've found a few guides online that say to un-comment and lines from Python-3.X.X/Modules/Setup and add the --openssldir=/usr/local/ssl flag to the ./configure step for OpenSSL. I do these in my dockerfile. This has had the effect that, during the ./configure output for Python, I see the following line.
checking for X509_VERIFY_PARAM_set1_host in libssl... yes
Yet I receive the following errors:
[91m*** WARNING: renaming "_ssl" since importing it failed: /usr/lib/x86_64-linux-gnu/libssl.so.1.1: version `OPENSSL_1_1_1' not found (required by build/lib.linux-x86_64-3.8/_ssl.cpython-38-x86_64-linux-gnu.so)
[0m[91m*** WARNING: renaming "_hashlib" since importing it failed: /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1: version `OPENSSL_1_1_1' not found (required by build/lib.linux-x86_64-3.8/_hashlib.cpython-38-x86_64-linux-gnu.so)
[0m
Python build finished successfully!
...
Following modules built successfully but were removed because they could not be imported:
_hashlib _ssl
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
If ./configure finds X509..., why am I still getting the hashlib and ssl errors?
The full Dockerfile, FWIW:
FROM jenkins/jenkins:lts
USER root
RUN apt-get update && apt-get install -y apt-utils gcc make zlib1g-dev \
build-essential libffi-dev checkinstall libsqlite3-dev
RUN wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz && \
tar xzf openssl-1.1.1d.tar.gz && \
cd openssl-1.1.1d && \
./config -Wl,--enable-new-dtags,-rpath,'$(LIBRPATH)' --prefix=/usr/local/ssl --openssldir=/usr/local/ssl && \
make && \
make test && \
make install
RUN wget -q https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz && \
tar -xzf Python-3.8.2.tgz && \
cd Python-3.8.2 && \
./configure && \
make && \
make install
USER jenkins
Following modules built successfully but were removed because they could not be imported:
_hashlib _ssl
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
It seems like installation issue when building openssl from source. For build failure on _ssl module, try extra options like --with-openssl, CFLAGS and LDFLAGS when configuring Python using the script ./configure, e.g.
./configure --with-openssl=/PATH/TO/YOUR/OPENSSL_INSTALL_FOLDER/ \
--enable-optimizations \
--with-ssl-default-suites=openssl \
CFLAGS="-I/PATH/TO/YOUR/OPENSSL_INSTALL_FODLER/include" \
LDFLAGS="-L/PATH/TO/YOUR/OPENSSL_INSTALL_FODLER/"
Also try this command openssl version, if it reports error like this :
/usr/lib/x86_64-linux-gnu/libssl.so.1.1: version `OPENSSL_1_1_1' not found
that means there is linking problem on your openssl library, I'm not sure if you're on Linux or other system, but for Linux system, you can manually modify the links to openssl library to fix the problem as described in my answer at here.
Reference
Building Python 3.7.1 - SSL module failed
Python 3.7.0 wont compile with SSL Support 1.1.0
I reckon that Jenkins Image comes with some openssl version installed that is not 1.1.1, hence you find X509... in libssl but cant build.
Regarding said config option, you can spin up the container with bash as CMD, copy the config from within the container to the machine where the Image lies, edit ist and bake your version of the config into the Image.
I had the same problem and after 3+ hours of searching THIS is what actually worked:
Error: "ssl module is not available" when installing package with pip3
———————————————————-
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError(“Can’t connect to HTTPS URL because the SSL module is not available.”)’: /simple/pip/
The fix:
———————————————————-
sudo yum install openssl11 openssl11-devel
mkdir /usr/local/openssl11
cd /usr/local/openssl11
ln -s /usr/lib64/openssl11 lib
ln -s /usr/include/openssl11 include
The above code creates an alternate path for the latest openssl11 and symlinks it in a place that matches the folder structure that python expects.
Then proceed with the original steps in the guide
(https://linuxstans.com/how-to-install-python-centos/)
and ./configure like this within your python install folder:
./configure -–enable-optimizations -–with-openssl=/usr/local/openssl11
make altinstall
the altinstall is important so that you don't overwrite the system default python. You'll have to invoke it as python3.10 from command line thereafter.
I tested this on CentOS 7 and Python-3.10.8.
ref:
https://linuxstans.com/how-to-install-python-centos/?unapproved=4338&moderation-hash=9903c8ebd6634e5bbbad96716e283f8b#comment-4338

python3.7 No module named pip

i had a problem with installing packages to new upgraded python to version 3.7
When i type:
python3.7 -m pip install pip -d
/usr/local/bin/python3.7: No module named pip
I make easy_install like this: sudo easy_install pip what solve previous problems, but now it create pip3.7 in a weird way. Whem i calling for pip3.8 version, this returns me a message:
pip3.7 -V
pip 19.2.3 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)
Someone know what i am doing wrong? I tried many things to resolve it. Reinstall, purge, install with symbolic link from python2 etc.
There is a script which i used to install python3.7 (i had also a problem with zlib):
sudo cd /home
sudo wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
sudo tar xf Python-3.7.3.tar.xz
sudo cd ./Python-3.7.3/
sudo ./configure
sudo make
sudo make install
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3 10
python3 --version
Where is a mistake?
Answer finally i find myself. There: https://linuxize.com/post/how-to-install-python-3-7-on-debian-9/
There with similar errors you can recompile it, even without removal (on secound machine i checked without removal) failed installation before.
If someone want to good upgrade with everywith working on debian 9. I recommend that script:
NOW_DIR=$(pwd)
apt update
apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
mkdir ~/python_upgrade
cd ~/python_upgrade
curl -O https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
tar -xf Python-3.7.3.tar.xz
cd Python-3.7.3
./configure --enable-optimizations
make -j $(nproc)
make altinstall
python3.7 --version
cd $NOW_DIR
rm -rf ~/python_upgrade
There are minor changes from that commands in tutorial. Run above file.sh as root.
It can take some times, on my virtual machine with 2 cores during another job it takes 40 minutes. I hope someone find there good answer, not totally green comments like 'apt install' which is first try action before looking to web.

Python 3 No module named '_ssl'

The Problem
While I run you python3 application, it shows
File "/usr/local/lib/python3.6/ssl.py", line 101, in <module>
import _ssl # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
What I've tried
install the dependencies
yum install openssl-devel
I also edited the setup.py file and recomplie python3
# Detect SSL support for the socket module (via _ssl)
search_for_ssl_incs_in = [
'/usr/local/ssl/include',
'/usr/local/include/openssl', #I've added this line
'/usr/contrib/ssl/include/'
]
I've complied the openssl with the path configuration
#tar -xzvf openssl-***.tar.gz
#./config --prefix=/usr/local --openssldir=/usr/local/openssl
#make & make install
CentOS 7
Python 3.6
I found some solution:
if you use centos,try:
s1
yum install openssl-devel -y
then when you compile, append --with-ssl,just like this
./configure prefix=/usr/local/share/python3 --with-ssl
s2
-- install depend library, make share compile is fluent
yum install -y zlib zlib-devel openssl-devel sqlite-devel bzip2-devel libffi libffi-devel gcc gcc-c++
(ubuntu)sudo apt-get install libz-dev
wget --no-check-certificate http://www.openssl.org/source/openssl-1.1.1.tar.gz
tar -zxvf openssl-1.1.1.tar.gz
cd openssl-1.1.1
./config --prefix=$HOME/openssl shared zlib
make && make install
-- configure shared ld library path so that compile can find it
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/openssl/lib" >> $HOME/.bash_profile
source $HOME/.bash_profile
(zsh user has some different with .zsh_profile)
-- compile with openssl path
./configure prefix=/usr/local/share/python3 --with-openssl=$HOME/openssl
I faced the same issue, I installed python from source and didn't enabled ssl option while compiling. So I find the solution in the following article. You need to find ssl section Modules/Setup.dist and uncomment that section. Hope this will help someone.

How to compile mod_wsgi 4.5.3 with Python3.5.2 under centos7, Apache/2.4.6

I tried ./configure for mod_wsgi 4.5.3 like below:
./configure --with-python=/opt/Python352/bin
Where /opt/Python352 folder is installed with python3.5.2.
However in CentOS 7 the "configure" always builds the mod_wsgi with binary "python"(corresponding to python2.75) not with binary "python3" or "python3.5".
Requirement is to get the .so file, the latest for mod_wsgi 4.5.3, compiled using python3.5.2 and load this in Apache/2.4.6 under CentOS 7.
Thanks.
Here's an exact dump of what I use for CentOS 7.2. You can probably do without a lot of the pre-reqs, but I figured I'd include them since many are handy to have:
echo 'Python 3.5.2 is not installed, installing Python 3 pre-requisites...'
yum -y groupinstall development
echo 'Installing extra packages for Python...'
yum -y install zlib-devel openssl-devel sqlite-devel bzip2-devel python-devel openssl-devel libffi-devel openssl-perl libjpeg-turbo-devel zlib-devel giflib ncurses-devel gdbm-devel xz-devel tkinter readline-devel tk tk-devel
echo 'Installing Python 3.5.2...'
wget -q 'https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz'
tar -xzf 'Python-3.5.2.tgz'
cd ./Python-3.5.2
CXX=g++ ./configure --enable-shared
make
echo 'Moving to alternate location to keep system Python version intact...'
make altinstall
cd ..
rm Python-3.5.2.tgz
rm -rf ./Python-3.5.2
ln -fs /usr/local/bin/python3.5 /usr/bin/python3.5
echo "/usr/local/lib/python3.5" > /etc/ld.so.conf.d/python35.conf
echo "/usr/local/lib" >> /etc/ld.so.conf.d/python35.conf
ldconfig
echo 'Now, install mod_wsgi...'
wget -q "https://github.com/GrahamDumpleton/mod_wsgi/archive/4.4.21.tar.gz"
tar -xzf '4.4.21.tar.gz'
cd ./mod_wsgi-4.4.21
./configure --with-python=/usr/local/bin/python3.5
make
make install
I couldn't see an obvious problem with your question given the information, but figured a complete working example would help.

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.
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 :)
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.
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.
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
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.
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)
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
Rebuilding Python fails
./configure --with-zlib-dir=/usr/local/lib
...
configure: WARNING: unrecognized options: --with-zlib-dir
...
Solution
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")])
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
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.
sudo apt-get install zlib1g-dev
is what worked for me.
For anyone having the same error on macOS Mojave, this is the easiest solution for installing/linking the header files:
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
Then just build Python again as usual (also works with pyenv builds).
This is how I've built Python 3.7 on a CentOS 7 machine without devel libraries and installed it into user's ~/.local without sudo. zlib, OpenSSL and readline modules are built.
Download Python source in .tgz format from https://www.python.org/downloads. Download zlib-devel-1.2.7-18.el7.x86_64.rpm, openssl-devel-1.0.2k-19.el7.x86_64.rpm, krb5-devel-1.15.1-50.el7.x86_64.rpm, libcom_err-devel-1.42.9-19.el7.x86_64.rpm, readline-devel-6.2-11.el7.x86_64.rpm from https://centos.pkgs.org for SSL, zlib and readline modules. Put all files into $DIST_PATH.
cd ~
DIST_PATH=<path to downloaded files>
for f in $DIST_PATH/*.rpm; do rpm2cpio $f | cpio -idmv; done
mkdir ~/usr/lib
# symlinks in ~/usr/lib64 are broken, so create new links to system libraries in ~/usr/lib and pass this folder to ./configure
for f in ~/usr/lib64/*.so; do ln -s /lib64/`readlink $f` ~/usr/lib/`basename $f`; done
tar -xzf $DIST_PATH/Python-3.7.13.tgz && cd Python-3.7.13
# That machine has devtoolset-7 with newer version GCC
scl enable devtoolset-7 bash
# curly brackets are important here, otherwise LDFLAGS is -LOME/usr/lib
./configure --enable-optimizations --prefix=$HOME/.local --with-openssl=$HOME/usr CPPFLAGS='-I${HOME}/usr/include' LDFLAGS='-L${HOME}/usr/lib'
make
# (!) altinstall is used, use python3.7 command to access newly built Python
make altinstall
rm -rf ~/usr
Links:
Unpacking RPM packages
krb5.h error
com_err.h
CPPFLAGS, LDFLAGS

Categories