首页 > 解决方案 > 如何在 Ubuntu docker 镜像中安装 Python2.7.5?

问题描述

我有在 Ubuntu 中安装Python 2.7.5的特定要求,我可以毫无问题地安装 2.7.18

下面是我的dockerfile

ARG UBUNTU_VERSION=18.04
FROM ubuntu:$UBUNTU_VERSION

RUN apt-get update -y \
    && apt-get install -y python2.7.x \
    && rm -rf /var/lib/apt/lists/*

ENTRYPOINT ["python"]

但是,如果我将其设置为python2.7.5

ARG UBUNTU_VERSION=18.04
FROM ubuntu:$UBUNTU_VERSION

RUN apt-get update -y \
    && apt-get install -y python2.7.5 \
    && rm -rf /var/lib/apt/lists/*

ENTRYPOINT ["python"]

它抛出以下错误

E:通过正则表达式“python2.7.5”找不到任何包

我想安装 Python 2.7.5 以及相关的 PIP,我该怎么办?

标签: pythondockerpython-2.7ubuntu

解决方案


此版本不再在规范镜像中可用。

它已于 2013 年发布。

因此,同时拥有两者pythonpip 从那时起一起工作是具有挑战性的。

centos7 上的 Python 2.7.5 + PIP

ubuntu如果不是必需的,这可能是最简单的方法。

ARG CENTOS_VERSION=7
FROM centos:$CENTOS_VERSION

# Python 2.7.5 is installed with centos7 image
# Add repository for PIP
RUN yum install -y epel-release

# Install pip
RUN yum install -y python-pip

RUN python --version

ENTRYPOINT [ "python" ]

Ubuntu 上的 Python 2.7.5

我已经能够从源代码安装它

安装不成功pip

https://bootstrap.pypa.io/pip/2.7/get-pip.py

ARG UBUNTU_VERSION=18.04
FROM ubuntu:$UBUNTU_VERSION

ARG PYTHON_VERSION=2.7.5

# Install dependencies
# PIP - openssl version > 1.1 may be an issue (try older ubuntu images)
RUN apt-get update \
  && apt-get install -y wget gcc make openssl libffi-dev libgdbm-dev libsqlite3-dev libssl-dev zlib1g-dev \
  && apt-get clean

WORKDIR /tmp/

# Build Python from source
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
  && tar --extract -f Python-$PYTHON_VERSION.tgz \
  && cd ./Python-$PYTHON_VERSION/ \
  && ./configure --enable-optimizations --prefix=/usr/local \
  && make && make install \
  && cd ../ \
  && rm -r ./Python-$PYTHON_VERSION*

RUN python --version

ENTRYPOINT [ "python" ]

ubuntu 上的 Python 2.7.6 + pip

Ubuntu 14.04 仍然有镜像工作(多长时间???)。

Python 包真的很接近你的期望。

您可以尝试使用该脚本运行您的脚本。

ARG UBUNTU_VERSION=14.04
FROM ubuntu:$UBUNTU_VERSION

RUN apt-get update \
  && apt-get install -y python python-pip \
  && apt-get clean

RUN python --version

ENTRYPOINT [ "python" ]

Python 2.7.5 + pip,不工作,但可以在 ubuntu 上

这是我尝试过的,但没有成功。

ARG UBUNTU_VERSION=16.04
FROM ubuntu:$UBUNTU_VERSION


# Install dependencies
RUN apt-get update \
  && apt-get install -y wget gcc make openssl libffi-dev libgdbm-dev libsqlite3-dev libssl-dev zlib1g-dev \
  && apt-get clean

WORKDIR /tmp/

# Build python from source
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
  && tar --extract -f Python-$PYTHON_VERSION.tgz \
  && cd ./Python-$PYTHON_VERSION/ \
  && ./configure --enable-optimizations --prefix=/usr/local \
  && make && make install \
  && cd ../ \
  && rm -r ./Python-$PYTHON_VERSION*

# Build pip from source
RUN wget https://bootstrap.pypa.io/pip/2.7/get-pip.py \
    && python get-pip.py

RUN python --version

ENTRYPOINT [ "python" ]

带有 pip 的 Python 2.7.9 - 根据评论中的要求

你可以使用这个 dockerfile,构建 python 包括 pip。

ARG UBUNTU_VERSION=16.04
FROM ubuntu:$UBUNTU_VERSION

ARG PYTHON_VERSION=2.7.9

# Install dependencies
RUN apt-get update \
  && apt-get install -y wget gcc make openssl libffi-dev libgdbm-dev libsqlite3-dev libssl-dev zlib1g-dev \
  && apt-get clean

WORKDIR /tmp/

# Build Python from source
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
  && tar --extract -f Python-$PYTHON_VERSION.tgz \
  && cd ./Python-$PYTHON_VERSION/ \
  && ./configure --with-ensurepip=install --enable-optimizations --prefix=/usr/local \
  && make && make install \
  && cd ../ \
  && rm -r ./Python-$PYTHON_VERSION*

RUN python --version \
  && pip --version

ENTRYPOINT [ "python" ]

推荐阅读