~/python/python-how-apply-patch-install-pyenv.md

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

1 min readMarch 03, 2021by Gorav Singal#python#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 

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

How to Install Python from command line and Docker on Windows

How to Install Python from command line and Docker on Windows

Introduction We will see how we can install Python 3.7 on Windows without UI. i…

How to Install Python from command line and Docker on Linux

How to Install Python from command line and Docker on Linux

Introduction We will see how we can install Python from command line using pyenv…

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

Python Build Quality Gates with Pylint and Unittest Coverage Thresholds

Python Build Quality Gates with Pylint and Unittest Coverage Thresholds

Introduction It is very important to introduce few process so that your code and…

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Introduction We often require to execute in timed manner, i.e. to specify a max…

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…