GNU make:检查是否需要安装 python 模块

GNU make: check whether a python module needs to be installed

我在 Ubuntu bionic 上使用 GNU make,我需要检查是否需要安装 Ubuntu python 包 apt-get

我在我的 Makefile 中试过这个来捕获 python 导入错误 (ModuleNotFoundError):

MY_PYTHON     := $(shell echo `which python3`)
MY_SETUPTOOLS := $(shell $(MY_PYTHON) -c 'import setuptools' )

ifneq ($(findstring /python3,$(MY_PYTHON)),/python3)
$(info 1. Installing python)
TMP := $(shell apt-get -y install python python3)
MY_PYTHON := $(shell echo `which python3`)
endif

ifeq ($(findstring ModuleNotFoundError,$(MY_SETUPTOOLS)),ModuleNotFoundError)
$(info 1. Installing setuptools)
TMP := $(shell apt-get -y install python-setuptools python3-setuptools)
endif

.PHONY: test
test:
        cd $(THIS_DIR); cd tests; py.test -s test_mytools.py
        make clean

当我 运行 sudo make test 时,我看到下面的错误打印到终端;但它不会触发使用 apt-get:

安装 python3-setuptools 的逻辑
$ sudo make test
make[2]: Entering directory '/home/mpennington/foo'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'setuptools'

如何解决此问题以便 make 安装 python3-setuptools

解决方法是在我检查 python3-setuptools:

时将 stderr 重定向到 stdout
MY_SETUPTOOLS := $(shell $(MY_PYTHON) -c 'import setuptools' 2>&1 )
                                                             ^^^^