Search code examples
pythonwindowsmacosmakefilegnu-make

Getting base Python interpreter in Makefile on Windows or POSIX


I am trying to make a Makefile that has the path to system Python in an environment variable, that works on Windows and POSIX systems.

The below works on macOS and Windows WSL, but if you have a virtual environment activated, it returns the venv's path (not system Python's path):

SYSTEM_PYTHON = $(shell which python3)

The below works regardless of venv being activated, but it doesn't work on Windows (due to Windows not using bin, and using \\):

SYSTEM_PYTHON = $(shell python3 -c "import sys; print(sys.base_prefix)")/bin/python3

How can I make a SYSTEM_PYTHON env var that works on both Windows and POSIX, regardless if a virtual environment is previously activated?


Solution

  • this works on my windows even if i had an venv

    ifeq ($(OS),Windows_NT)
        SYSTEM_PYTHON = $(shell python -c "import sys; print(sys.base_prefix)")\\python.exe
    else
        SYSTEM_PYTHON = $(shell python3 -c "import sys; print(sys.base_prefix)")/bin/python3
    endif
    
    all:
        @echo "System Python is located at: $(SYSTEM_PYTHON)"