diff --git a/src/diffpy/srreal/__init__.py b/src/diffpy/srreal/__init__.py index 2beff97..ce99d60 100644 --- a/src/diffpy/srreal/__init__.py +++ b/src/diffpy/srreal/__init__.py @@ -14,6 +14,20 @@ ############################################################################## """Tools for real space structure analysis.""" +# Windows DLL loading fix for Python 3.8+ +# On Windows, add the conda library bin directory to DLL search path +# before importing extension modules that depend on DLLs +import os +import sys + +if sys.platform == "win32" and sys.version_info >= (3, 8): + # Try to add conda library bin directory to DLL search path + conda_prefix = os.environ.get("CONDA_PREFIX") + if conda_prefix: + lib_bin_dir = os.path.join(conda_prefix, "Library", "bin") + if os.path.isdir(lib_bin_dir): + os.add_dll_directory(lib_bin_dir) + # package version from diffpy.srreal.version import __version__ diff --git a/tests/test_dll_loading.py b/tests/test_dll_loading.py new file mode 100644 index 0000000..eefbc12 --- /dev/null +++ b/tests/test_dll_loading.py @@ -0,0 +1,31 @@ +"""Unit tests for Windows DLL loading initialization.""" + +import os +import sys + + +def test_import_succeeds(): + """Ensure diffpy.srreal can be imported successfully on all platforms.""" + import diffpy.srreal + + assert hasattr(diffpy.srreal, "__version__") + + +def test_windows_dll_directory_handling(): + """Verify Windows DLL directory handling doesn't raise errors.""" + # This test verifies that the Windows-specific DLL directory + # initialization in __init__.py doesn't cause issues on any platform. + + # The actual DLL directory logic is executed during module import, + # so if we got this far, it succeeded. + + # On Windows with Python 3.8+, verify the logic would have run + if sys.platform == "win32" and sys.version_info >= (3, 8): + # Check that CONDA_PREFIX environment variable handling works + conda_prefix = os.environ.get("CONDA_PREFIX") + if conda_prefix: + lib_bin_dir = os.path.join(conda_prefix, "Library", "bin") + # The directory should exist in a proper conda environment + # but we don't assert this as it may not exist in all test + # environments + assert isinstance(lib_bin_dir, str)