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.
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
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
:If your Python 2 scripts need additional package dependencies installed via
pip
, you'll need to instead use a custom Dockerfile: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 the2to3
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.
Note that by default, Python 3 is installed to
/usr/bin/python3
so add thepython-is-python3
package to makepython
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 towebimage_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:Note: a custom Dockerfile will override
webimage_extra_packages
configurations in.ddev/config.yaml
.Thank you so much for this guide — I’m doing a
yarn install
with a set-up that mandates Python 2, and also needsmake
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.