From 4cf8f5759fe715b0defb0ba998ac1c354b814cca Mon Sep 17 00:00:00 2001
From: "Davis E. King" <davis@dlib.net>
Date: Sun, 10 Aug 2025 21:09:12 -0400
Subject: [PATCH] update python packaging to work with newer toolchains (#3102)

---
 README.md                   | 17 ++++++-----------
 pyproject.toml              |  3 ++-
 setup.py                    | 18 +++++++++---------
 tools/python/CMakeLists.txt |  7 -------
 4 files changed, 17 insertions(+), 28 deletions(-)

diff --git a/README.md b/README.md
index 79cc041d2b..8b6ba5364e 100644
--- a/README.md
+++ b/README.md
@@ -37,22 +37,17 @@ vcpkg install dlib
 
 ## Compiling dlib Python API
 
-Before you can run the Python example programs you must install the build requirement.
+Either fetch the latest stable release of dlib from PyPi and install that:
 ```bash
-python -m venv venv
-pip install build
+pip install dlib
 ```
-
-Then you must compile dlib and install it in your environment. Type:
+Or fetch the very latest version from github and install that:
 ```bash
-python -m build --wheel
-pip install dist/dlib-<version>.whl
+git clone https://github.com/davisking/dlib.git
+cd dlib
+pip install .
 ```
 
-Or download dlib using PyPi:
-```bash
-pip install dlib
-```
 
 ## Running the unit test suite
 
diff --git a/pyproject.toml b/pyproject.toml
index 9787c3bdf0..2a301221f0 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,3 +1,4 @@
 [build-system]
-requires = ["setuptools", "wheel"]
+requires = ["setuptools", "wheel", "packaging"]
 build-backend = "setuptools.build_meta"
+
diff --git a/setup.py b/setup.py
index 9c0e57347f..5053e0e255 100644
--- a/setup.py
+++ b/setup.py
@@ -28,17 +28,21 @@
 import os
 import re
 import sys
+import errno
+import stat
 import shutil
 import platform
 import subprocess
 import multiprocessing
-from distutils import log
-from math import ceil,floor
+from math import floor
 
+from packaging.version import Version, parse as parse_version
 from setuptools import find_packages, setup, Extension
 from setuptools.command.build_ext import build_ext
-from distutils.version import LooseVersion
+import logging
 
+logging.basicConfig(level=logging.INFO)
+log = logging.getLogger("setup")
 
 def get_extra_cmake_options():
     """read --clean, --no, --set, --compiler-flags, and -G options from the command line and add them as cmake switches.
@@ -90,7 +94,7 @@ def get_extra_cmake_options():
 
 class CMakeExtension(Extension):
     def __init__(self, name, sourcedir=''):
-        Extension.__init__(self, name, sources=[])
+        super().__init__(name, sources=[])
         self.sourcedir = os.path.abspath(sourcedir)
 
 def rmtree(name):
@@ -160,7 +164,7 @@ def get_cmake_version(self):
     def run(self):
         cmake_version = self.get_cmake_version()
         if platform.system() == "Windows":
-            if LooseVersion(cmake_version) < '3.1.0':
+            if parse_version(cmake_version) < Version('3.1.0'):
                 sys.stderr.write("\nERROR: CMake >= 3.1.0 is required on Windows\n\n")
                 sys.exit(1)
 
@@ -236,10 +240,6 @@ def read_version_from_cmakelists(cmake_file):
     patch = re.findall("set\\(CPACK_PACKAGE_VERSION_PATCH.*\"(.*)\"", open(cmake_file).read())[0]
     return major + '.' + minor + '.' + patch
 
-def read_entire_file(fname):
-    """Read text out of a file relative to setup.py.  """
-    return open(os.path.join(fname)).read()
-
 setup(
     name='dlib',
     version=read_version_from_cmakelists('dlib/CMakeLists.txt'),
diff --git a/tools/python/CMakeLists.txt b/tools/python/CMakeLists.txt
index 349f8f3f83..a7aadb0d79 100644
--- a/tools/python/CMakeLists.txt
+++ b/tools/python/CMakeLists.txt
@@ -27,13 +27,6 @@ if (POLICY CMP0042)
 endif()
 
 
-# To avoid dll hell, always link everything statically when compiling in
-# visual studio.  This way, the resulting library won't depend on a bunch
-# of other dll files and can be safely copied to someone else's computer
-# and expected to run.
-if (MSVC)
-    include(${CMAKE_CURRENT_LIST_DIR}/../../dlib/cmake_utils/tell_visual_studio_to_use_static_runtime.cmake)
-endif()
 
 add_subdirectory(../../dlib/external/pybind11 pybind11_build)
 add_subdirectory(../../dlib dlib_build)
