Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Windows DLL search path for Python 3.8+ #1440

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
21 changes: 21 additions & 0 deletions python/MaterialX/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# Python 3.8+ on Windows: DLL search paths for dependent
# shared libraries
# Refs.:
# - https://github.com/python/cpython/issues/80266
# - https://docs.python.org/3.8/library/os.html#os.add_dll_directory
import os
import sys
if sys.platform == "win32" and sys.version_info >= (3, 8):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we handle Python less than 3.8 by appending to PATH or sys.path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not. It was never done before and this code only became necessary in 3.8 when accessing PATH for library lookups was disabled because it was quite a large security risk.

import importlib.metadata
try:
importlib.metadata.version('MaterialX')
except importlib.metadata.PackageNotFoundError:
# On a non-pip installation, this file is in %INSTALLDIR%\python\MaterialX
# We need to add %INSTALLDIR%\bin to the DLL path.
mxdir = os.path.dirname(__file__)
pydir = os.path.split(mxdir)[0]
installdir = os.path.split(pydir)[0]
bindir = os.path.join(installdir, "bin")
if os.path.exists(bindir):
os.add_dll_directory(bindir)

from .main import *
from .colorspace import *

Expand Down