[英]Creating a docker container that runs Ubuntu with Python 3.6+ and Pip
I'm trying to install Python 3.6 or above with pip in an docker container that runs Ubuntu.我正在尝试将 Python 3.6 或更高版本与 pip 安装在运行 Z3D4645423F8CE69 的 docker 容器中I've tried quite a few things with no success
我已经尝试了很多没有成功的事情
FROM ubuntu:18.04
RUN apt update
RUN apt install software-properties-common -y
RUN add-apt-repository ppa:deadsnakes/ppa
RUN ln -s /usr/bin/pip3 /usr/bin/pip
RUN ln -s /usr/bin/python3.8 /usr/bin/python
RUN apt install python3.8 -y
RUN apt install pip
RUN pip install auto-sklearn
RUN pip install pandas
ADD test.py /
CMD [ "python", "./test.py" ]
This returns "Unable to locate package pip."这将返回“无法找到 package pip”。 I tried removing "apt install pip" incase Python 3.8 comes with it, but it gives me the error: "pip: not found."
我尝试删除“apt install pip”,以防 Python 3.8 附带它,但它给了我错误:“pip:未找到”。
FROM ubuntu:18.04
RUN apt install software-properties-common -y
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update
RUN install python3-pip
RUN pip install auto-sklearn
RUN pip install pandas
ADD test.py /
CMD [ "python", "./test.py" ]
This installs pip, but auto-sklearn requires Python version 3.6 or higher and this installs a lower version.这会安装 pip,但 auto-sklearn 需要 Python 版本 3.6 或更高版本,而这会安装较低版本。 Auto-sklearn requires Linux as well which is why I'm using "FROM ubuntu" rather than "FROM python" cause "FROM python" seems to build a container on whatever native OS is running on the computer building the container, which for me is Windows.
Auto-sklearn 也需要 Linux 这就是为什么我使用“FROM ubuntu”而不是“FROM python”的原因,因为“FROM python”似乎是在构建容器的计算机上运行的任何本机操作系统上构建容器,这对我来说是 Windows。
I see two consecutive issues here, so let's address them accordingly:我在这里看到两个连续的问题,所以让我们相应地解决它们:
This returns "Unable to locate package pip."
这将返回“无法找到 package pip”。 I tried removing "apt install pip" incase Python 3.8 comes with it, but it gives me the error: "pip: not found."
我尝试删除“apt install pip”,以防 Python 3.8 附带它,但它给了我错误:“pip:未找到”。
That's right.这是正确的。 If you inspect the contents of the
/usr/bin
directory of the pulled image, you will notice that there is no pip
or pip3
there.如果您检查拉取的镜像的
/usr/bin
目录的内容,您会注意到那里没有pip
或pip3
。 So RUN ln -s /usr/bin/pip3 /usr/bin/pip
line in your Dockerfile does nothing.所以
RUN ln -s /usr/bin/pip3 /usr/bin/pip
行中的 Dockerfile 什么都不做。 Even when python3.6
gets installed in the container (after calling apt install software-properties-common -y
), you do not get pip
with it.即使在容器中安装了
python3.6
(在调用apt install software-properties-common -y
之后),您也不会得到pip
。
The following commands can be used to install python3.6
binary and the corresponding pip
:以下命令可用于安装
python3.6
二进制文件和相应的pip
:
RUN apt-get update
RUN apt-get install python3-pip
This installs both python3.6
and pip3
in the /usr/bin
directory of your ubuntu:18/04
container.这会将
python3.6
和pip3
安装在ubuntu:18/04
容器的/usr/bin
目录中。
Even if you manage to get both python3.6
and pip
for python3.6
, installation of auto-sklearn
might still fail with the following error:即使您设法同时获得
python3.6
和pip
的python3.6
,安装auto-sklearn
仍可能会失败并出现以下错误:
RuntimeError: Python version >= 3.7 required.
This is because some of the dependencies (eg ConfigSpace
package) require python version >= 3.7.这是因为某些依赖项(例如
ConfigSpace
包)需要 python 版本 >= 3.7。
This answer explains how to install pip
for python3.8
on Ubuntu: https://stackoverflow.com/a/63207387/15043192这个答案解释了如何在 Ubuntu 上为
python3.8
安装pip
: https://stackoverflow.com/a/632092737
You can follow it or get both pip
and python3.8
installed using the following sequence:您可以按照它或使用以下顺序安装
pip
和python3.8
:
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt update
RUN apt install python3.8
RUN apt install python3-pip
Now if you execute python3.6 -m pip --version
in the container, you would get something like (the version of pip
might be different):现在,如果你在容器中执行
python3.6 -m pip --version
,你会得到类似的东西( pip
的版本可能不同):
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
Note: here we use previously installed pip
for python3.6
to install pip
for python3.8
.注意:这里我们使用之前安装的
pip
for python3.6
来安装pip
for python3.8
。 Do not ask me why:-)别问我为什么:-)
RUN python3.8 -m pip install pip --upgrade
RUN python3.8 -m pip install auto-sklearn
Note: the command above might also install pandas
package among other dependencies of auto-sklearn
.注意:上面的命令可能还会安装
pandas
package 以及auto-sklearn
的其他依赖项。
This would change the default这将更改默认值
RUN ln -s /usr/bin/python3.8 /usr/bin/python
Now if you execute python -m pip --version
in the container, you would get something like (the version of pip
might be different):现在,如果你在容器中执行
python -m pip --version
,你会得到类似的东西( pip
的版本可能不同):
pip 21.2.4 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)
In the end, your Dockerfile should look like this:最后,您的 Dockerfile 应该如下所示:
FROM ubuntu:18.04
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt update
RUN apt install python3.8
RUN apt install python3-pip
RUN python3.8 -m pip install auto-sklearn
RUN python3.8 -m pip install pandas
RUN ln -s /usr/bin/python3.8 /usr/bin/python
ADD test.py /
CMD [ "python", "./test.py" ]
To avoid messing around with different versions of python
and pip
, you might want to have a look into virtual environments .为避免混淆
python
和pip
的不同版本,您可能需要查看虚拟环境。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.