diff --git a/src/bindings/python/CMakeLists.txt b/src/bindings/python/CMakeLists.txt index d3c79db98a..8588700a0c 100644 --- a/src/bindings/python/CMakeLists.txt +++ b/src/bindings/python/CMakeLists.txt @@ -227,16 +227,22 @@ target_compile_definitions(PyOpenColorIO PY_VERSION_PATCH=${Python_VERSION_PATCH} ) +# Set to site-package location. if(WIN32) set(_Python_VARIANT_PATH "${CMAKE_INSTALL_LIBDIR}/site-packages") else() set(_Python_VARIANT_PATH "${CMAKE_INSTALL_LIBDIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages") endif() -# Create an internal global variable to access it in another scope but not publicly visible -# using ccmake. +# Create an internal global variable to access it in another scope but not publicly visible. +# The site-package location is needed in setup_ocio.bat.in and setup_ocio.sh.in. set(PYTHON_VARIANT_PATH ${_Python_VARIANT_PATH} CACHE INTERNAL "") +# Set to PyOpenColorIO site-package location. +set(_PyOpenColorIO_SITE_PACKAGE_DIR "${PYTHON_VARIANT_PATH}/PyOpenColorIO") + install(TARGETS PyOpenColorIO - LIBRARY DESTINATION ${_Python_VARIANT_PATH} + LIBRARY DESTINATION ${_PyOpenColorIO_SITE_PACKAGE_DIR} ) + +install(FILES __init__.py DESTINATION ${_PyOpenColorIO_SITE_PACKAGE_DIR}) \ No newline at end of file diff --git a/src/bindings/python/__init__.py b/src/bindings/python/__init__.py new file mode 100644 index 0000000000..3ce17e051e --- /dev/null +++ b/src/bindings/python/__init__.py @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright Contributors to the OpenColorIO Project. + +import os, sys, platform + +# +# Python 3.8+ has stopped loading DLLs from PATH environment variable on Windows. +# +# This code reproduce the old behavior (loading DLLs from PATH) by doing the following: +# 1 - Tokenizing PATH +# 2 - Checking that the directories exist and are not "." +# 3 - Add them to the DLL load path. +# +# The behavior described above is opt-out which means that it is activated by default. +# A user can opt-out and use the default behavior of Python 3.8+ by setting OCIO_PYTHON_LOAD_DLLS_FROM_PATH +# environment variable to 0. +# + +if sys.version_info >= (3, 8) and platform.system() == "Windows" and os.getenv("OCIO_PYTHON_LOAD_DLLS_FROM_PATH", "1") == "1": + for path in os.getenv("PATH", "").split(os.pathsep): + if os.path.exists(path) and path != ".": + os.add_dll_directory(path) + +from .PyOpenColorIO import * \ No newline at end of file