I’m trying to run python 3 scripts as part of a Makefile target that can be used on Linux or Windows (msys). What is the correct way it invoke Python 3 when the system may also have Python 2.7 installed?
It seems that:
python script.py doesn’t work, because on systems that have Python 2.7 installed, that points at the 2.7 version.
python3 script.py doesn’t work, because if you only have Python 3 installed it names the executable
python.exe on Windows.
py -3 script.py doesn’t work on Linux because the
py shunt only exists on Windows.
Is there a good example out there of how to do this correctly?
I don’t think there is a general solution which will work out of the box without user cooperation. The usual way to do this is to specify a reasonable default in your
Makefile and allow the user to override it. Perhaps something like
PY3 = python3 py_ver := $(shell $(PY3) -c 'import sys; print(sys.version_info.major)') ifneq ($(py_ver),3) $(error Need PY3 to point to a working Python 3 executable) endif # now use $(PY3) wherever you previously used python
Now you’d use
make PY3=/usr/local/bin/python or whatever to run this with a different path for the binary.
If you use Automake there are built-in macros for Python 2 and Python 3; see also AM_PATH_PYTHON for python2 and python3