Implements #24024 for python2 and python3.
Depends on #24599
Depends on #25039
Depends on #23733
Component: build
Keywords: destdir python2 python3
Author: Erik Bray
Branch/Commit: eca6471
Reviewer: Julian Rüth
Issue created by migration from https://trac.sagemath.org/ticket/25043