python|March 03, 2021|1 min read

Python - How to apply patch to Python and Install Python via Pyenv

TL;DR

Apply a custom patch (e.g., FIPS) to a Python source and install the patched version through pyenv using environment variables and the pyenv install command.

Python - How to apply patch to Python and Install Python via Pyenv

Introduction

In this post, we will see how we can apply a patch to Python and install it through pyenv.

Python Patch for FIPS (as example)

We will take example of FIPS patch to python 3.9.2, as in post

Install via Pyenv

We are doing it for Centos-7.

First, we will set some environment variable.

PYENV_VERSION=3.9.2
PYENV_INSTALLER_URL=https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer
PYTHON_CONFIGURE_OPTS="--enable-shared"

Lets download pyenv installer.

umask 022
curl -s -S -L "$PYENV_INSTALLER_URL" -o /usr/bin/pyenv-installer
chmod 0755 /usr/bin/pyenv-installer

Installing Pyenv

/usr/bin/pyenv-installer
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"

Apply patch (assuming we have patch from post) and Install Python 3.9.2

pyenv install --patch $PYENV_VERSION < python_patch_3.9.2.patch
pyenv global $PYENV_VERSION 

Its clean and easier way to install Python through pyenv.

Dockerfile

Lets do it via Dockerfile

FROM centos:7

RUN yum makecache fast && yum -y update

RUN yum -y install git \
    libffi-devel libffi libssh2-devel autoconf automake libtool \
    libxml2-devel libxslt-devel libjpeg-devel zlib-devel \
    make cmake gcc python-devel python-setuptools wget \
    && yum clean all \
    && rm -rf /var/cache/yum

ADD python_patch_3.9.2.patch /python_installation/
ARG PYENV_VERSION=3.9.2
ENV PYENV_INSTALLER_URL=https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer
ENV PYTHON_CONFIGURE_OPTS="--enable-shared"
RUN umask 022 \
 && curl -s -S -L "$PYENV_INSTALLER_URL" -o /usr/bin/pyenv-installer \
 && chmod 0755 /usr/bin/pyenv-installer \
 && /usr/bin/pyenv-installer \
 && eval "$(pyenv init -)" \
 && pyenv install --patch $PYENV_VERSION < /python_installation/python_patch_3.9.2.patch \
 && pyenv global $PYENV_VERSION 

Related Posts

How to renew SSL certificate from Lets-encrypt when your website is using cloudflare

How to renew SSL certificate from Lets-encrypt when your website is using cloudflare

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Problem Statement I’ve been using image styles, and heavily used “Scale and crop…

How to create Repository using Github Rest API, Configure Visibility and Assign a Team as Readonly

How to create Repository using Github Rest API, Configure Visibility and Assign a Team as Readonly

Introduction I had to create many repositories in an Github organization. I…

Drupal 8 Comment module - How to configure comments module from ugly to beautiful - Theming comments module

Drupal 8 Comment module - How to configure comments module from ugly to beautiful - Theming comments module

Introduction Drupal provides a powerful comment module, which comes as a part of…

Drupal 8 - How to show view user data to owner user only

Drupal 8 - How to show view user data to owner user only

Introduction to Problem I have some data related to users. For example: which…

Explaining issue&#58; response to preflight request doesn't pass access control check

Explaining issue&#58; response to preflight request doesn't pass access control check

You are developing a nodejs web application having some UI and backend APIs…

Latest Posts

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…

Deep Dive on Caching: From Browser to Database

Deep Dive on Caching: From Browser to Database

“There are only two hard things in Computer Science: cache invalidation and…

System Design Patterns for Real-Time Updates at High Traffic

System Design Patterns for Real-Time Updates at High Traffic

The previous articles in this series covered scaling reads and scaling writes…

System Design Patterns for Scaling Writes

System Design Patterns for Scaling Writes

In the companion article on scaling reads, we covered caching, replicas, and…

System Design Patterns for Managing Long-Running Tasks

System Design Patterns for Managing Long-Running Tasks

Introduction Some operations simply can’t finish in the time a user is willing…