skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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-image/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.


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search