From a2d70e0ec845fc31a3c54072d16076aae0470d52 Mon Sep 17 00:00:00 2001 From: Zach Lewis Date: Wed, 8 Jan 2025 07:53:06 -0500 Subject: [PATCH 1/4] feat: Disable loading Python DLLs from PATH by default on Windows Users can enable the legacy behavior by setting env var `OIIO_PYTHON_LOAD_DLLS_FROM_PATH=1`. This commit also changes the environment variable name from `OIIO_LOAD_DLLS_FROM_PATH` to `OIIO_PYTHON_LOAD_DLLS_FROM_PATH` to match the convention used by OCIO. Signed-off-by: Zach Lewis --- src/python/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/python/__init__.py b/src/python/__init__.py index f3c345ec13..72ac6cac8a 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -7,8 +7,8 @@ # This works around the python 3.8 change to stop loading DLLs from PATH on Windows. # We reproduce the old behaviour by manually tokenizing PATH, checking that the directories exist and are not ".", # then add them to the DLL load path. -# This behviour can be disabled by setting the environment variable "OIIO_LOAD_DLLS_FROM_PATH" to "0" -if sys.version_info >= (3, 8) and platform.system() == "Windows" and os.getenv("OIIO_LOAD_DLLS_FROM_PATH", "1") == "1": +# This behviour can be enabled by setting the environment variable "OIIO_PYTHON_LOAD_DLLS_FROM_PATH" to "1" +if sys.version_info >= (3, 8) and platform.system() == "Windows" and os.getenv("OIIO_PYTHON_LOAD_DLLS_FROM_PATH", "0") == "1": for path in os.getenv("PATH", "").split(os.pathsep): if os.path.exists(path) and path != ".": os.add_dll_directory(path) From 5061dd6c537c0af38ad8cfdb06a920ee87c3fad5 Mon Sep 17 00:00:00 2001 From: Zach Lewis Date: Mon, 13 Jan 2025 12:39:12 -0500 Subject: [PATCH 2/4] ci: Windows / Py3.9: enable legacy "load DLLs from path" behavior Set OIIO_PYTHON_LOAD_DLLS_FROM_PATH=1 in the `Windows-2022 VS2022` task's matrix. I hope the windows runners can understand the "export" command... Signed-off-by: Zach Lewis --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9825d82abe..d24afcbe22 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -594,3 +594,4 @@ jobs: vsver: 2022 generator: "Visual Studio 17 2022" python_ver: "3.9" + setenvs: export OIIO_PYTHON_LOAD_DLLS_FROM_PATH=1 From 1a3694b179a878dba4b1ddf331b458cfa94eb203 Mon Sep 17 00:00:00 2001 From: Zach Lewis Date: Mon, 13 Jan 2025 15:38:47 -0500 Subject: [PATCH 3/4] fix: Use OPENIMAGEIO_ env var prefix and document DLL loading behavior OPENIMAGEIO_PYTHON_LOAD_DLLS_FROM_PATH instead of OIIO_PYTHON_LOAD_DLLS_FROM_PATH. Signed-off-by: Zach Lewis --- .github/workflows/ci.yml | 2 +- INSTALL.md | 9 +++++++++ src/doc/imageioapi.rst | 14 ++++++++++++++ src/python/__init__.py | 4 ++-- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d24afcbe22..87a36a6311 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -594,4 +594,4 @@ jobs: vsver: 2022 generator: "Visual Studio 17 2022" python_ver: "3.9" - setenvs: export OIIO_PYTHON_LOAD_DLLS_FROM_PATH=1 + setenvs: export OPENIMAGEIO_PYTHON_LOAD_DLLS_FROM_PATH=1 diff --git a/INSTALL.md b/INSTALL.md index e6f807ff8a..0f99249fa6 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -357,6 +357,15 @@ produce a dynamic-linked version. 3. Execute the PowerShell command from where vcpkg is located in directory. ``vcpkg install openimageio`` + +**Note: Importing the OpenImageIO Python Module** + +As of OpenImageIO 3.0.3.0, the default DLL-loading behavior for Python 3.8+ has changed. + +If you've built OIIO from source and ``import OpenImageIO`` is throwing a ModuleNotFound exception, revert to the legacy DLL-loading behavior by setting environment variable +``OPENIMAGEIO_PYTHON_LOAD_DLLS_FROM_PATH=1``. + + Test Images ----------- diff --git a/src/doc/imageioapi.rst b/src/doc/imageioapi.rst index f294024d80..9bb7eaa142 100644 --- a/src/doc/imageioapi.rst +++ b/src/doc/imageioapi.rst @@ -384,3 +384,17 @@ inside the source code. name and version of the software and an indecipherable hash of the command line, but not the full human-readable command line. (This was added in OpenImageIO 2.5.11.) + +.. cpp:var:: OPENIMAGEIO_PYTHON_LOAD_DLLS_FROM_PATH + + Windows only. Mimics the DLL-loading behavior of Python 3.7 and earlier. + If set to "1", all directories under ``PATH`` will be added to the DLL load + path before attempting to import the OpenImageIO module. (This was added in + OpenImageIO 3.0.3.0) + + Note: This "opt-in-style" behavior replaces and inverts the "opt-out-style" + Windows DLL-loading behavior governed by the now-defunct `OIIO_LOAD_DLLS_FROM_PATH` + environment variable (added in OpenImageIO 2.4.0/2.3.18). + + In other words, to reproduce the default Python-module-loading behavior of + earlier versions of OIIO, set ``OPENIMAGEIO_PYTHON_LOAD_DLLS_FROM_PATH=1``. diff --git a/src/python/__init__.py b/src/python/__init__.py index 72ac6cac8a..19f4559877 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -7,8 +7,8 @@ # This works around the python 3.8 change to stop loading DLLs from PATH on Windows. # We reproduce the old behaviour by manually tokenizing PATH, checking that the directories exist and are not ".", # then add them to the DLL load path. -# This behviour can be enabled by setting the environment variable "OIIO_PYTHON_LOAD_DLLS_FROM_PATH" to "1" -if sys.version_info >= (3, 8) and platform.system() == "Windows" and os.getenv("OIIO_PYTHON_LOAD_DLLS_FROM_PATH", "0") == "1": +# This behviour can be enabled by setting the environment variable "OPENIMAGEIO_PYTHON_LOAD_DLLS_FROM_PATH" to "1" +if sys.version_info >= (3, 8) and platform.system() == "Windows" and os.getenv("OPENIMAGEIO_PYTHON_LOAD_DLLS_FROM_PATH", "0") == "1": for path in os.getenv("PATH", "").split(os.pathsep): if os.path.exists(path) and path != ".": os.add_dll_directory(path) From 7e9c4887aa75972fdcb91f7e2e9c4fe138b30729 Mon Sep 17 00:00:00 2001 From: Zach Lewis Date: Tue, 14 Jan 2025 12:08:50 -0500 Subject: [PATCH 4/4] ci: set Win VS2019 CI environ to load DLLs for Py3.9 Signed-off-by: Zach Lewis --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e01b007841..2e38f90651 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -589,6 +589,7 @@ jobs: vsver: 2019 generator: "Visual Studio 16 2019" python_ver: "3.9" + setenvs: export OPENIMAGEIO_PYTHON_LOAD_DLLS_FROM_PATH=1 - desc: Windows-2022 VS2022 runner: windows-2022 vsver: 2022