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

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

Related

Pyenv unable to install older versions of Python 3 due to `configure: error: C compiler cannot create executables`

I've been trying to use pyenv to install older versions of python3. I've tried 3.0.1, 3.1.0, 3.1.1, and 3.1.2. I kept getting the error
ImportError: No module named _ssl
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?
Following some documentation from pyenv I tried updating my command to explicitly pass the location of the headers and libraries like so:
➜ CPPFLAGS="-I/usr/bin/openssl/include" \
LDFLAGS="-L/usr/bin/openssl/lib" \
pyenv install -v 3.1.2
That seems to have gotten past that error but now I'm getting the error configure: error: C compiler cannot create executables
The only suggestion I can find for Ubuntu is to install/update the build-essential package which I've done and was already installed and fully updated.
I can see that the gcc command is working. So what else am I missing?
Full Error
➜ CPPFLAGS="-I/usr/bin/openssl/include" \
LDFLAGS="-L/usr/bin/openssl/lib" \
pyenv install -v 3.1.2
/tmp/python-build.20230107155624.97473 ~/GitProjects/VWAP_backtest
Downloading Python-3.1.2.tar.gz...
-> https://www.python.org/ftp/python/3.1.2/Python-3.1.2.tgz
/tmp/python-build.20230107155624.97473/Python-3.1.2 /tmp/python-build.20230107155624.97473 ~/GitProjects/VWAP_backtest
Installing Python-3.1.2...
patching file ./setup.py
patching file ./Lib/ssl.py
patching file ./Modules/_ssl.c
checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... linux5
checking machine type as reported by uname -m... x86_64
checking for --without-gcc... no
checking for gcc... gcc
checking for C compiler default output file name...
configure: error: C compiler cannot create executables
See `config.log' for more details.
BUILD FAILED (Ubuntu 22.04 using python-build 2.3.9-18-g25c974d5)
Looks like the libssl-dev package is missing. Try installing that library along with some others you might have missed by using the pyenv build instructions.
In particular you should install the pyenv dependencies. For example for the Debian OS you would want:
sudo apt update; sudo apt install build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

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>.

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

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.

Unable to pip install psycopg2 on linux

While trying to create a new Heroku Python (Django) app on Linux Mint I encounter following error when pip tries to install psycopg2
x86_64-linux-gnu-gcc: error: unrecognized command line option ‘-fstack-protector-strong’
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
All dependencies are met (as explained in this question) so I don't see the problem here..
I have found the solution. It might be helpful for others as I didn't really find the answer online..
1) The error originates from an outdated gcc version (4.8) on my system. This verion does not support the '-fstack-protector-...' command line option.
2) I installed a newer version (4.9) as follows:
$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$ sudo apt-get update
$ sudo apt-get install gcc-4.9
3) I removed the symlink x86_64-linux-gnu-gcc which was located in /usr/bin and replaced it with a symlink to the newly installed gcc-4.9 version
$ sudo rm x86_64-linux-gnu-gcc
$ sudo ln -s gcc-4.9 x86_64-linux-gnu-gcc
It took me a while to figure this out, I hope it helps others
Can't upvote DeBaze's answer (not enough rep).
I'm using Ubuntu 16.02.
I only needed to install the latest gcc and create the symlink ...
sudo apt-get install gcc-4.9
sudo ln -s gcc-4.9 x86_64-linux-gnu-gcc

Categories