Python 3 No module named '_ssl' - python

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.

Related

Installing guided LDA package [duplicate]

I am trying to build a shared library using a C extension file but first I have to generate the output file using the command below:
gcc -Wall utilsmodule.c -o Utilc
After executing the command, I get this error message:
> utilsmodule.c:1:20: fatal error: Python.h: No such file or directory
compilation terminated.
I have tried all the suggested solutions over the internet but the problem still exists. I have no problem with Python.h. I managed to locate the file on my machine.
Looks like you haven't properly installed the header files and static libraries for python dev. Use your package manager to install them system-wide.
For apt (Ubuntu, Debian...):
sudo apt-get install python-dev # for python2.x installs
sudo apt-get install python3-dev # for python3.x installs
For yum (CentOS, RHEL...):
sudo yum install python-devel # for python2.x installs
sudo yum install python3-devel # for python3.x installs
For dnf (Fedora...):
sudo dnf install python2-devel # for python2.x installs
sudo dnf install python3-devel # for python3.x installs
For zypper (openSUSE...):
sudo zypper in python-devel # for python2.x installs
sudo zypper in python3-devel # for python3.x installs
For apk (Alpine...):
# This is a departure from the normal Alpine naming
# scheme, which uses py2- and py3- prefixes
sudo apk add python2-dev # for python2.x installs
sudo apk add python3-dev # for python3.x installs
For apt-cyg (Cygwin...):
apt-cyg install python-devel # for python2.x installs
apt-cyg install python3-devel # for python3.x installs
Note: python3-dev does not automatically cover all minor versions of python3, if you are using e.g. python 3.8 you may need to install python3.8-dev.
On Ubuntu, I was running Python 3 and I had to install
sudo apt-get install python3-dev
If you want to use a version of Python that is not linked to python3, install the associated python3.x-dev package. For example:
sudo apt-get install python3.5-dev
For Python 3.7 and Ubuntu in particular, I needed
sudo apt install libpython3.7-dev
.
I think at some point names were changed from pythonm.n-dev to this.
for Python 3.6, 3.8 through 3.10 (and counting…) similarly:
sudo apt install libpython3.6-dev 
sudo apt install libpython3.8-dev 
sudo apt install libpython3.9-dev
sudo apt install libpython3.10-dev
Two things you have to do.
Install development package for Python, in case of Debian/Ubuntu/Mint it's done with command:
sudo apt-get install python-dev
Second thing is that include files are not by default in the include path, nor is Python library linked with executable by default. You need to add these flags (replace Python's version accordingly):
-I/usr/include/python2.7 -lpython2.7
In other words your compile command ought to be:
gcc -Wall -I/usr/include/python2.7 -lpython2.7 utilsmodule.c -o Utilc
on Fedora run this for Python 2:
sudo dnf install python2-devel
and for Python 3:
sudo dnf install python3-devel
If you are using tox to run tests on multiple versions of Python, you may need to install the Python dev libraries for each version of Python you are testing on.
sudo apt-get install python2.6-dev
sudo apt-get install python2.7-dev
etc.
Make sure that the Python dev files come with your OS.
You should not hard code the library and include paths. Instead, use pkg-config, which will output the correct options for your specific system:
$ pkg-config --cflags --libs python2
-I/usr/include/python2.7 -lpython2.7
You may add it to your gcc line:
gcc -Wall utilsmodule.c -o Utilc $(pkg-config --cflags --libs python2)
For me, changing it to this worked:
#include <python2.7/Python.h>
I found the file /usr/include/python2.7/Python.h, and since /usr/include is already in the include path, then python2.7/Python.h should be sufficient.
You could also add the include path from command line instead - gcc -I/usr/lib/python2.7 (thanks #erm3nda).
Solution for Cygwin
You need to install the package python2-devel or python3-devel, depending on the Python version you're using.
You can quickly install it using the 32-bit or 64-bit setup.exe (depending on your installation) from Cygwin.com.
Example (modify setup.exe's filename and Python's major version if you need):
$ setup.exe -q --packages=python3-devel
You can also check my other answer for a few more options to install Cygwin's packages from the command-line.
In AWS API (centOS) its
yum install python27-devel
AWS EC2 install running python34:
sudo yum install python34-devel
If you use a virtualenv with a 3.6 python (edge right now), be sure to install the matching python 3.6 dev sudo apt-get install python3.6-dev, otherwise executing sudo python3-dev will install the python dev 3.3.3-1, which won't solve the issue.
In my case, what fixed it in Ubuntu was to install the packages libpython-all-dev (or libpython3-all-dev if you use Python 3).
It's not the same situation, but it also works for me and now I can use SWIG with Python3.5:
I was trying to compile:
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
With Python 2.7 works fine, not with my version 3.5:
existe_wrap.c:147:21: fatal error: Python.h: No existe el archivo o el
directorio compilation terminated.
After run in my Ubuntu 16.04 installation:
sudo apt-get install python3-dev # for python3.x installs
Now I can compile without problems Python3.5:
gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/
I managed to solve this issue and generate the .so file in one command
gcc -shared -o UtilcS.so
-fPIC -I/usr/include/python2.7 -lpython2.7 utilsmodule.c
I also encountered this error when I was installing coolprop in ubuntu.
For ubuntu 16.04 with python 3.6
sudo apt-get install python3.6-dev
If ever this doesn't work try installing/updating gcc lib.
sudo apt-get install gcc
try apt-file. It is difficult to remember the package name where the missing file resides. It is generic and useful for any package files.
For example:
root#ubuntu234:~/auto# apt-file search --regexp '/Python.h$'
pypy-dev: /usr/lib/pypy/include/Python.h
python2.7-dbg: /usr/include/python2.7_d/Python.h
python2.7-dev: /usr/include/python2.7/Python.h
python3.2-dbg: /usr/include/python3.2dmu/Python.h
python3.2-dev: /usr/include/python3.2mu/Python.h
root#ubuntu234:~/auto#
Now you can make an expert guess as to which one to choose from.
This problem can also arrive when you have different Python versions installed and you use a pip that's not the system's one. In that case, the non-system pip won't find the right version of Python headers.
It happened to me when trying to pip install a package for a Python bundled with an application. As it was not system's python, apt install pythonXX-dev didn't work.
In this case, the solution is to find the right python header:
find / -iname 'Python.h'
In the output, you will see system python headers, and hopefully the one you are looking for, for example:
/usr/include/python3.7m/Python.h
/usr/include/python3.6m/Python.h
/home/ubuntu/workspace/blender-git/lib/linux_centos7_x86_64/python/include/python3.7m/Python.h
/home/ubuntu/miniconda3/pkgs/python-3.8.5-h7579374_1/include/python3.8/Python.h
/home/ubuntu/miniconda3/pkgs/python-3.7.0-h6e4f718_3/include/python3.7m/Python.h
/home/ubuntu/miniconda3/include/python3.8/Python.h
/home/ubuntu/miniconda3/envs/sim/include/python3.7m/Python.h
/home/ubuntu/src/blender-deps/Python-3.7.7/Include/Python.h
/opt/lib/python-3.7.7/include/python3.7m/Python.h
Then, you can set a compiler flag that will get used by gcc when called by pip.
Mine was /home/ubuntu/workspace/blender-git/lib/linux_centos7_x86_64/python/include/python3.7m/Python.h, so I did:
export CPPFLAGS=-I/home/ubuntu/src/blender-deps/Python-3.7.7/Include
pip install <package>
For CentOS 7:
sudo yum install python36u-devel
I followed the instructions here for installing python3.6 on several VMs: https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming-environment-on-centos-7
and was then able to build mod_wsgi and get it working with a python3.6 virtualenv
For the OpenSuse comrades out there:
sudo zypper install python3-devel
Here is yet another solution, because none of these solutions worked for me. For reference, I was trying to pip install something on an Amazon Linux AMI base Docker image for Python 3.6.
Non-docker solution:
# Install python3-devel like everyone says
yum -y install python36-devel.x86_64
# Find the install directory of `Python.h`
rpm -ql python36-devel.x86_64 | grep -i "Python.h"
# Forcefully add it to your include path
C_INCLUDE_PATH='/usr/include/python3.6m'
export C_INCLUDE_PATH
Docker solution:
# Install python3-devel like everyone says
RUN yum -y install python36-devel.x86_64
# Find the install directory of `Python.h`, for me it was /usr/include/python3.6m
RUN rpm -ql python36-devel.x86_64 | grep -i "Python.h" && fake_command_so_docker_fails_and_shows_us_the_output
# Since the previous command contains a purposeful error, remove it before the next run
# Forcefully add it to your include path
ARG C_INCLUDE_PATH='/usr/include/python3.6m'
NOTE: If you're getting the error when compiling C++, use CPLUS_INCLUDE_PATH.
Alternatively, you may prefer to use another Docker image. For example, I was trying to install asyncpg~=0.24.0 on python:3.9.4-slim, which generated the same error as you saw. However, when I updated the image to python:3, it worked fine.
If you're using Python 3.6 on Amazon Linux (based on RHEL, but the RHEL answers given here didn't work):
sudo yum install python36-devel
You must install the Python development files on your operating system if the Python provided with your operating system does not come with them. The many answers on this question show the myriad ways this can be achieved on different systems.
When you have done so, the problem is telling the compiler where they're located and how to compile against them. Python comes with a program called python-config. For compilation, you need the --includes output and for linking a program against the Python library (embedding Python into your program) the --ldflags output. Example:
gcc -c mypythonprogram.c $(python3-config --includes)
gcc -o program mypythonprogram.o $(python3-config --ldflags)
The python-config program can be named after the Python versions - on Debian, Ubuntu for example these can be named python3-config or python3.6-config.
Sure python-dev or libpython-all-dev are the first thing to (apt )install, but if that doesn't help as was my case, I advice you to install the foreign Function Interface packages by sudo apt-get install libffi-dev and sudo pip install cffi.
This should help out especially if you see the error as/from c/_cffi_backend.c:2:20: fatal error: Python.h: No such file or directory.
try locate your Python.h:
gemfield#ThinkPad-X1C:~$ locate Python.h
/home/gemfield/anaconda3/include/python3.7m/Python.h
/home/gemfield/anaconda3/pkgs/python-3.7.6-h0371630_2/include/python3.7m/Python.h
/usr/include/python3.8/Python.h
if not found, then install python-dev or python3-dev; else include the correct header path for compiler:
g++ -I/usr/include/python3.8 ...
I am on Ubuntu. I have installed all packages as was recommended in some answers.
sudo apt-get install python-dev # for python2.x installs
sudo apt-get install python3-dev # for python3.x installs
I still had this problem, the line:
#include "Python.h"
And some others, I can edit them manually, it is a bad practice.
I know the secret now, it comes from the cython source code. I have the file. It compiles without errors. That is the file.
Change PYTHON to python version you have, python/python3. Change FILE to your c-filename. The name of the makefile file should be Makefile. Run the the file with the command:
make all
Makefile for creating our standalone Cython program
FILE := file.c
PYTHON := python3
PYVERSION := $(shell $(PYTHON) -c "import sys;
print(sys.version[:3])")
PYPREFIX := $(shell $(PYTHON) -c "import sys; print(sys.prefix)")
INCDIR := $(shell $(PYTHON) -c "from distutils import sysconfig;
print(sysconfig.get_python_inc())")
PLATINCDIR := $(shell $(PYTHON) -c "from distutils import
sysconfig; print(sysconfig.get_python_inc(plat_specific=True))")
LIBDIR1 := $(shell $(PYTHON) -c "from distutils import sysconfig;
print(sysconfig.get_config_var('LIBDIR'))")
LIBDIR2 := $(shell $(PYTHON) -c "from distutils import sysconfig;
print(sysconfig.get_config_var('LIBPL'))")
PYLIB := $(shell $(PYTHON) -c "from distutils import sysconfig;
print(sysconfig.get_config_var('LIBRARY')[3:-2])")
CC := $(shell $(PYTHON) -c "import distutils.sysconfig;
print(distutils.sysconfig.get_config_var('CC'))")
LINKCC := $(shell $(PYTHON) -c "import distutils.sysconfig;
print(distutils.sysconfig.get_config_var('LINKCC'))")
LINKFORSHARED := $(shell $(PYTHON) -c "import distutils.sysconfig;
print(distutils.sysconfig.get_config_var('LINKFORSHARED'))")
LIBS := $(shell $(PYTHON) -c "import distutils.sysconfig;
print(distutils.sysconfig.get_config_var('LIBS'))")
SYSLIBS := $(shell $(PYTHON) -c "import distutils.sysconfig;
print(distutils.sysconfig.get_config_var('SYSLIBS'))")
.PHONY: paths all clean test
paths:
#echo "PYTHON=$(PYTHON)"
#echo "PYVERSION=$(PYVERSION)"
#echo "PYPREFIX=$(PYPREFIX)"
#echo "INCDIR=$(INCDIR)"
#echo "PLATINCDIR=$(PLATINCDIR)"
#echo "LIBDIR1=$(LIBDIR1)"
#echo "LIBDIR2=$(LIBDIR2)"
#echo "PYLIB=$(PYLIB)"
#echo "CC=$(CC)"
#echo "LINKCC=$(LINKCC)"
#echo "LINKFORSHARED=$(LINKFORSHARED)"
#echo "LIBS=$(LIBS)"
#echo "SYSLIBS=$(SYSLIBS)"
$(FILE:.c=): $(FILE:.c=.o)
$(LINKCC) -o $# $^ -L$(LIBDIR1) -L$(LIBDIR2) -l$(PYLIB)
$(LIBS) $(SYSLIBS) $(LINKFORSHARED)
$(FILE:.c=.o): $(FILE)
$(CC) -c $^ -I$(INCDIR) -I$(PLATINCDIR)
all: $(FILE:.c=)
This error occurred when I attempted to install ctds on CentOS 7 with Python3.6. I did all the tricks mentioned here including yum install python34-devel. The problem was Python.h was found in /usr/include/python3.4m but not in /usr/include/python3.6m. I tried to use --global-option to point to include dir (pip3.6 install --global-option=build_ext --global-option="--include-dirs=/usr/include/python3.4m" ctds). This resulted in a lpython3.6m not found when linking ctds.
Finally what worked was fixing the development environment for Python3.6 needs to correct with the include and libs.
yum -y install https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/python36u-libs-3.6.3-1.ius.centos7.x86_64.rpm
Python.h needs to be in your include path for gcc. Whichever version of python is used, for example if it's 3.6, then it should be in /usr/include/python3.6m/Python.h typically.
Sometimes even after installing python-dev the error persists,
Check for the error if it is 'gcc' missing.
First download as stated in https://stackoverflow.com/a/21530768/8687063, then install gcc
For apt (Ubuntu, Debian...):
sudo apt-get install gcc
For yum (CentOS, RHEL...):
sudo yum install gcc
For dnf (Fedora...):
sudo dnf install gcc
For zypper (openSUSE...):
sudo zypper in gcc
For apk (Alpine...):
sudo apk gcc
It often appear when you trying to remove python3.5 and install python3.6.
So when using python3 (which python3 -V => python3.6) to install some packages required python3.5 header will appear this error.
Resolve by install python3.6-dev module.
This means that Python.h isn't in your compiler's default include paths. Have you installed it system-wide or locally? What's your OS?
You could use the -I<path> flag to specify an additional directory where your compiler should look for headers. You will probably have to follow up with -L<path> so that gcc can find the library you'll be linking with using -l<name>.

How to fix "ssl module in Python is not available" in CentOs

New Python 3.7.1 installation on GoDaddy VPS CentOs 7.
Attempt pip3 install virtualenv or python 3 -m pip install virtualenv and get:
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
openssl-devel installed and up to date
This question has been asked and answered many times, but the solutions I've found have not solved my problem.
Thank you all!
I tried the CentOs and Linux-based solutions in the following:
"SSL module in Python is not available" when installing package with pip3
# To allow for building python ssl libs
yum install openssl-devel
# Download the source of any python version
cd /usr/src
wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
tar xf Python-3.7.1.tar.xz
cd Python-3.7.1
# Configure the build w/ your installed libraries
./configure
# Install into /usr/local/bin/python3.6, don't overwrite global python bin
make altinstall
Trying to install packages with Python 3.7.2 pip causes TSL/SSL errors
"SSL module in Python is not available" when installing package with pip3
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
pip cannot confirm SSL certificate: SSL module is not available
"ssl module in Python is not available"
and
pip cannot confirm SSL certificate: SSL module is not available
`uncommented suggestions for CentOs
make install failed:
gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -I. -I./Include -DUSE_SSL -I/include -I/include/openssl -c ./Modules/_ssl.c -o Modules/_ssl.o
./Modules/_ssl.c:74:6: error: #error "libssl is too old and does not support X509_VERIFY_PARAM_set1_host()"
./Modules/_ssl.c: In function ‘_ssl_configure_hostname’:
./Modules/_ssl.c:861: error: implicit declaration of function ‘SSL_get0_param’
./Modules/_ssl.c:861: warning: initialization makes pointer from integer without a cast
./Modules/_ssl.c:863: error: implicit declaration of function ‘X509_VERIFY_PARAM_set1_host’
./Modules/_ssl.c:869: error: implicit declaration of function ‘X509_VERIFY_PARAM_set1_ip’
./Modules/_ssl.c: In function ‘_ssl__SSLContext_impl’:
./Modules/_ssl.c:2988: error: ‘X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS’ undeclared (first use in this function)
./Modules/_ssl.c:2988: error: (Each undeclared identifier is reported only once
./Modules/_ssl.c:2988: error: for each function it appears in.)
./Modules/_ssl.c:3093: error: implicit declaration of function ‘SSL_CTX_get0_param’
./Modules/_ssl.c:3093: warning: assignment makes pointer from integer without a cast
./Modules/_ssl.c:3099: error: implicit declaration of function ‘X509_VERIFY_PARAM_set_hostflags’
./Modules/_ssl.c: In function ‘get_verify_flags’:
./Modules/_ssl.c:3397: warning: assignment makes pointer from integer without a cast
./Modules/_ssl.c: In function ‘set_verify_flags’:
./Modules/_ssl.c:3410: warning: assignment makes pointer from integer without a cast
./Modules/_ssl.c: In function ‘set_host_flags’:
./Modules/_ssl.c:3573: warning: assignment makes pointer from integer without a cast
make: *** [Modules/_ssl.o] Error 1`
https://www.tomordonez.com/pip-install-ssl-module-python-is-not-available.html
Here is a working solution for Python 3.7.9 and CentOS 7.8.2003:
su - root
yum install gcc openssl-devel bzip2-devel libffi-devel zlib-devel -y
cd /usr/src
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
tar xzf Python-3.7.9.tgz
cd Python-3.7.9
In /usr/src/Python-3.7.9/Modules/Setup.dist, uncomment these 4 lines:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
And finally:
./configure --enable-optimizations
make altinstall
I have seen this error when the version of openssl is incompatible. This is more likely when you're installing a newer Python (such as 3.7.2) on a not-as-recent version of the Linux system.
I would check to see what version of SSL libraries are expected with this version of Python and if necessary, install them locally.
This is a basic description of building the SSL libraries: DreamHost instructions for Installing Local OpenSSL Version
You will want to build Python again with the following option added to your invocation of ./config:
--with-openssl=/path/to/your/local/install
This works for me on CentOS 7.9 with Python 3.10.9 based on https://bugs.python.org/issue47201
This edits configure instead of Modules/Setup:
sudo yum install -y epel
sudo yum install -y openssl11-devel
wget https://www.python.org/ftp/python/3.10.9/Python-3.10.9.tgz
tar zxvf Python-3.10.9.tgz
cd Python-3.10.9
sed -i 's/PKG_CONFIG openssl /PKG_CONFIG openssl11 /g' configure
./configure --enable-optimizations
sudo make altinstall
# The following should run without errors if SSL was properly compiled
python3.10 -m ssl
Minor correction to a prev answer by Kevin Lemaire
Here is a working solution for Python 3.7.9 and CentOS 7.8.2003:
su - root
yum install gcc openssl-devel bzip2-devel libffi-devel zlib-devel -y
cd /usr/src
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
tar xzf Python-3.7.9.tgz
cd Python-3.7.9
In Modules/Setup, uncomment these 4 lines: <<< minor correction
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
And finally:
./configure --enable-optimizations
make altinstall
Please read this article
Basically you need to install openssl first and uncomment ssl related lines in Modules/Setup file in python source code before to make install.
This worked to me on installing Python 3.8.
Taking inspiration from the two answers on this thread, downloading and extracting OpenSSL 1.1.1o and then running the following sequence of commands solved the problem for me.
su - root
yum install gcc openssl-devel bzip2-devel libffi-devel zlib-devel -y
cd /usr/src
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
tar xzf Python-3.7.9.tgz
cd Python-3.7.9
./configure --enable-optimizations --with-openssl=/path/to/your/local/install
make install
Solution: Python 3.6.2 instead of 3.7.1

Building Python 3.7.1 - SSL module failed

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

Caffe didn't see hdf5.h when compiling

I am having trouble when installing Caffe Deep Learning Framework on Python:
When I run make command at caffe directory, it says
hdf5.h:no such directory
The steps I have done:
Update and upgrade my Ubuntu Server
Install Python 2.7
Having all of the dependencies base on http://caffe.berkeleyvision.org/install_apt.html
Run cp cp Makefile.config.example Makefile.config
Uncomment cpu_only = 1 in Makefile.config
I will be grateful if someone can help me.
Error message:
CXX src/caffe/util/hdf5.cpp
in file include from src/caffe/util/hdf5.cpp:1:0:
./include/caffe/util/hdf5.hpp:6:18: fatal error: hdf5.h: No such file or directory
compilation terminated
Makefile:572 recipe for target '.build_release/src/caffe/util/hdf5.o'
failed Make:*** [.build_release/src/caffe/util/hdf5.o] Error 1
What is the version of your Ubuntu install? Try this. In your Makefile.config try to append /usr/include/hdf5/serial/ to INCLUDE_DIRS:
--- INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
+++ INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
and rename hdf5_hl and hdf5 to hdf5_serial_hl and hdf5_serial in the Makefile:
--- LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
+++ LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
More about the bug fix here.
This solution worked for me on the Ubuntu16.04LTS
sudo apt-get install libhdf5-10
sudo apt-get install libhdf5-serial-dev
sudo apt-get install libhdf5-dev
sudo apt-get install libhdf5-cpp-11
find /usr -iname "*hdf5.h*"
/usr/include/hdf5/serial/hdf5.h
export CPATH="/usr/include/hdf5/serial/"
Another case I've experienced with:
I was using Ubuntu 14.04 and installing hdf5-1.10.0.
I found hdf5.h was located in /usr/local/hdf5/include. Thus, I modified Makefile.config file by adding that location to INCLUDE_DIRS.
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include \
/usr/local/hdf5/include
I didn't rename anything in Makefile. It worked fine.
It did not work for me on Ubuntu16.04 LTS.
So I had to
sudo apt-get install libhdf5-10
sudo apt-get install libhdf5-serial-dev
sudo apt-get install libhdf5-dev
sudo apt-get install libhdf5-cpp-11
find /usr -iname "*hdf5.h*"
/usr/include/hdf5/serial/hdf5.h
Now do this
export CPATH="/usr/include/hdf5/serial/"
On RHEL7, I got tired of hunting for specific hdf5 RPMs and ran:
sudo yum install *hdf5*
and these are what I have:
hdf5-openmpi3-static-1.8.12-11.el7.x86_64
hdf5-1.8.12-11.el7.x86_64
hdf5-openmpi-static-1.8.12-11.el7.x86_64
hdf5-openmpi3-devel-1.8.12-11.el7.x86_64
hdf5-openmpi3-1.8.12-11.el7.x86_64
hdf5-mpich-devel-1.8.12-11.el7.x86_64
hdf5-devel-1.8.12-11.el7.x86_64
hdf5-openmpi-devel-1.8.12-11.el7.x86_64
hdf5-mpich-static-1.8.12-11.el7.x86_64
hdf5-mpich-1.8.12-11.el7.x86_64
hdf5-openmpi-1.8.12-11.el7.x86_64
Thanks to #loretoparisi, I was able to figure out where I had the header file I was missing and the problem went away.
$ find /usr -iname "*hdf5.h*"
/usr/include/openmpi-x86_64/hdf5.h
/usr/include/hdf5.h
/usr/include/mpich-x86_64/hdf5.h
/usr/include/openmpi3-x86_64/hdf5.h

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