简体   繁体   中英

Installing Python3.9 and pip in docker image (and not python 3.8)

I'm trying to build a docker image to run some tests, and I'm having trouble convincing ubuntu to install python 3.9 and pip, and NOT python3.8.

starting Docker unbuntu:latest, it's no problem to install python3.9, but there is no pip. Ok, I'll try doing what all the answers suggests:

apt-get install python3-pip

but this installs **all ** of python3.8, plus other junk. Now, it does work at this point, but it bugs me to have a completely unnecessary version of python in an image that I want to be as small as it can be.

No, uninstalling 3.8 afterwards doesn't work, it removes pip.

How can I get python3.9, with pip, and no python3.8?

I did see this: How to install pip for Python 3.9 on Ubuntu 20.04

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

And when you run the script, it complains about needing distutils. No problem:

sudo apt-get install python3-distutils

And what does python3-distutils depend on? Why, python3.8, of course!

ubuntu store system python package in /usr/lib/python3/dist-packages and share it for all python3 versions.

Inside an ubuntu 20.04 container ( docker run -ti ubuntu:20.04 ):

apt-get update
apt-get install software-properties-common
add-apt-repository ppa:deadsnakes/ppa
# Install py39 from deadsnakes repository
apt-get install python3-9
# Install pip from standard ubuntu packages
apt-get install python3-pip

Then you can invoke pip with python3.9 -m pip ... , but be careful, everything with be installed in /usr/lib/python3/dist-packages .

But if all you need is a dockerized version of python3.9, it would be better to use the official python docker image , available with debian or alpine (very light) as a base. Both use a standard lib/pythonx.y/site-packages .

By default these images defines entrypoint to the python interpreter, but you can override why bash or busybox sh (alpine).

Edit for debian

launchpad repositories are build on ubuntu distribution. Using on over distribution can lead in libraries incompatibilities.

In deadsnakes ubuntu/xenial repository, python3.9 requires libssl1.0.0 , but debian/buster offer libssl1.1 .

The easiest way to get python3.9 in debian / buster is to use the official image ( docker run -ti python:3.9-buster ). Otherwise, build it ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM