I need to execute a Python script inside the Ddev web docker image, but am having trouble figuring out what Debian python libraries are required to get Python binary with additional py package dependencies working.
Most of this is obsolete, because from DDEV v1.23.0 you can't easily get python 2 on DDEV at all, since it's been dropped from upstream. However, see @stasadev answer below for a great add-on that solves this. ddev add-on get stasadev/ddev-python2
, see https://github.com/stasadev/ddev-python2.
You really don’t want to be using Python 2 do you? (See caveats 1 & 2 below)
Add the following in .ddev/config.yml
:
webimage_extra_packages: [python]
If your Python 2 scripts need additional package dependencies installed via pip
, you'll need to instead use a custom Dockerfile:
ARG BASE_IMAGE
FROM $BASE_IMAGE
RUN apt-key adv --refresh-keys --keyserver keyserver.ubuntu.com \
&& apt update
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confold" --no-install-recommends --no-install-suggests python python-pip
RUN pip install somepackage anotherpackage
Note: a custom Dockerfile will override webimage_extra_packages
configurations in .ddev/config.yaml
.
Caveat 1: As of 2022, Ddev web image runs Debian 11 and sudo apt-get python
still installs Python 2. This may change in future versions of Debian, so be careful when you upgrade Ddev.
Caveat 2: Python 2 has reached its End Of Life and is unsupported. Additionally, important package manager pip
is no longer able to natively install (without workarounds) on latest Python 2, so you're probably better off upgrading your scripts to Python 3 using the 2to3
utility.
Use the following Ddev configuration to install Python 3 into /usr/bin/python along with most of any additional package dependencies for your py scripts.
webimage_extra_packages: [python3, python-is-python3]
Note that by default, Python 3 is installed to /usr/bin/python3
so add the python-is-python3
package to make python
execute Python 3.
You can also usually work around needing to install the python3-pip
package, because most Python 3 packages are already bundled for Debian. Therefore, additional Python 3 package dependencies can be added by comma-separated name to webimage_extra_packages
. See list of stable Python packages for Debian here.
If your dependencies are not bundled and you need to use pip
, Conda, or another python package manager, then you must implement a custom Dockerfile at .ddev/web-build/Dockerfile
like this:
ARG BASE_IMAGE
FROM $BASE_IMAGE
RUN apt-key adv --refresh-keys --keyserver keyserver.ubuntu.com \
&& apt update
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confold" --no-install-recommends --no-install-suggests python3 python-is-python3 python3-pip
RUN pip3 install somepackage anotherpackage
Note: a custom Dockerfile will override webimage_extra_packages
configurations in .ddev/config.yaml
.