Can I install python 3.7 in ubuntu 18.04 without having python 3.6 in the system?

jua*_*uan 5 ubuntu pip python-3.x docker

Please read the question carefully before closing as duplicate, I believe the use case to be unique.

I'm trying to create a docker image that only has python 3.7 installed, the problem is that if I try to install pip, the command also installs python 3.6 which I do not want.

The relevant part of the ideal docker file I'm tinkering is as follows

FROM ubuntu:18.04

# Upgrade installed packages
RUN apt-get update && apt-get upgrade -y && apt-get clean

# (...)

# Python package management and basic dependencies
RUN apt-get install -y python3.7 python3.7-dev python3.7-pip

# Register the version in alternatives
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1

# Set python 3 as the default python
RUN update-alternatives --set python /usr/bin/python3.7

# Upgrade pip to latest version
RUN python -m ensurepip --upgrade

# (...)

Run Code Online (Sandbox Code Playgroud)

This would fail as python3.7-pip doesn't seem to exist; only python3-pip does, which is what installs python 3.6 for some reason.

I tried not installing pip at all and doing it manually, like so

# (...)

RUN apt-get install -y python3.7 python3.7-dev

# (...)

RUN curl 'https://bootstrap.pypa.io/get-pip.py' > get-pip.py

RUN python get-pip.py pip --no-setuptools --no-wheel
Run Code Online (Sandbox Code Playgroud)

Which fails with this error:

Traceback (most recent call last):
  File "get-pip.py", line 21492, in <module>
    main()
  File "get-pip.py", line 197, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 82, in bootstrap
    import pip._internal
  File "/tmp/tmpbez2vju9/pip.zip/pip/_internal/__init__.py", line 40, in <module>
  File "/tmp/tmpbez2vju9/pip.zip/pip/_internal/cli/autocompletion.py", line 8, in <module>
  File "/tmp/tmpbez2vju9/pip.zip/pip/_internal/cli/main_parser.py", line 8, in <module>
  File "/tmp/tmpbez2vju9/pip.zip/pip/_internal/cli/cmdoptions.py", line 14, in <module>
ModuleNotFoundError: No module named 'distutils.util'
Run Code Online (Sandbox Code Playgroud)

Again, installing python3-distutils results in python 3.6 appearing in the system

那么,有没有一种方法可以在ubuntu 18.04中仅安装功能齐全的python 3.7,而不必安装python 3.6?

don*_*tor 21

如果其他人可以将安装 Python3.6 作为副作用(python3.7-distutils 介绍了它,正如 OP 所指出的那样)。这将安装 Python3.7 使其成为默认值,并pip使用您的 python3.7 安装获得最新版本

FROM ubuntu:18.04

# Upgrade installed packages
RUN apt-get update && apt-get upgrade -y && apt-get clean

# (...)

# Python package management and basic dependencies
RUN apt-get install -y curl python3.7 python3.7-dev python3.7-distutils

# Register the version in alternatives
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1

# Set python 3 as the default python
RUN update-alternatives --set python /usr/bin/python3.7

# Upgrade pip to latest version
RUN curl -s https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \
    python get-pip.py --force-reinstall && \
    rm get-pip.py


# (...)
Run Code Online (Sandbox Code Playgroud)

  • 不要将“python”用于 python 3 。否则你可能会破坏 apt,请参阅 /sf/ask/3014382591/ 。最好“RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1”并在各处用“python3”替换“python”。 (2认同)

asm*_*ier 5

@donhector 的解决方案有两个微妙的问题:

  1. 它只会安装过时的 Python 版本 3.7.5。您需要存储库ppa:deadsnakes才能获取更新版本
  2. 通过安装替代品,它打破了Python 2.x 所使用的python约定。python这可能会破坏 apt,请参阅如何在不破坏 apt 的情况下更新 Python 3 的替代方案?

没有这些问题的解决方案是:

FROM ubuntu:18.04

# Upgrade installed packages
RUN apt update && apt upgrade -y && apt clean

# install python 3.7.10 (or newer)
RUN apt update && \
    apt install --no-install-recommends -y build-essential software-properties-common && \
    add-apt-repository -y ppa:deadsnakes/ppa && \
    apt install --no-install-recommends -y python3.7 python3.7-dev python3.7-distutils && \
    apt clean && rm -rf /var/lib/apt/lists/*

# Register the version in alternatives (and set higher priority to 3.7)
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2

# Upgrade pip to latest version
RUN curl -s https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \
    python3 get-pip.py --force-reinstall && \
    rm get-pip.py
Run Code Online (Sandbox Code Playgroud)


小智 4

我看到两个选择:

  1. 使用 Ubuntu 映像,保持系统中的 Python 不变。安装 pyenv ( https://github.com/pyenv/pyenv ),然后下载 python 3.7 安装,与系统的 Python 完全分离。

或者

  1. 使用标记为 3.7.3-stretch 或 3.7.3-slim-stretch (Debian) 的官方 Python 映像