How to add Python and pip or conda packages to DDEV
Asked Answered
L

3

8

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.

Lulu answered 4/1, 2022 at 22:7 Comment(0)
L
8

Python 2 on Ddev

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.

Python 3 on Ddev

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.

Lulu answered 4/1, 2022 at 22:25 Comment(0)
M
6

Thank you so much for this guide -- I'm doing a yarn install with a set-up that mandates Python 2, and also needs make

Following your advice, I used:

webimage_extra_packages: [build-essential, python]

per https://techoverflow.net/2018/06/04/fixing-npm-node-gyp-error-not-found-make-on-ubuntu/

Next, I had another error related to my version of NodeJS being too new, so I downgraded that:

nodejs_version: "14"

Per the instructions here:

https://ddev.readthedocs.io/en/latest/users/cli-usage/#nodejs-npm-nvm-and-yarn

Finally everything installed correctly.

Morley answered 17/6, 2022 at 20:45 Comment(0)
D
1

Python 2 is no longer available in DDEV v1.23+ because the upstream image for Debian 12 Bookworm removed it from the repositories.

A workaround to install Python 2 is to use the add-on https://github.com/stasadev/ddev-python2

Drusilladrusus answered 30/8 at 8:46 Comment(1)
Thanks @Drusilladrusus a lot has changed since DDEV 1.23+ has been released and Python 3 is now the default. My answer above really needs an overhaul at this point and is completely outdated.Lulu

© 2022 - 2024 — McMap. All rights reserved.