Repository: numpy/numpy

Issue:
BUILD: clean up handling of dll loading on Windows
In ~#13109~ #13019 we forced preloading of support DLLs on windows. As per the discussion that starts with [this comment](https://github.com/numpy/numpy/pull/13019#issuecomment-467694625). We should move the code to a `numpy/_distributor_init.py` file in [MacPython/numpy-wheels](https://github.com/MacPython/numpy-wheels)  and wrap it in a try-except in case `__file__` does not exist.

This issue should be resolved before 1.17

xref gh-12994


Discussion:
Could I work on this ?
@abhinavsagar this is not the best issue to work on for a new contributor, and @mattip is on it already
Got it.

Resolving patch:
diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py
--- a/numpy/core/__init__.py
+++ b/numpy/core/__init__.py
@@ -5,29 +5,6 @@
 
 import os
 
-# on Windows NumPy loads an important OpenBLAS-related DLL
-# and the code below aims to alleviate issues with DLL
-# path resolution portability with an absolute path DLL load
-if os.name == 'nt':
-    from ctypes import WinDLL
-    import glob
-    # convention for storing / loading the DLL from
-    # numpy/.libs/, if present
-    libs_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
-                                             '..', '.libs'))
-    DLL_filenames = []
-    if os.path.isdir(libs_path):
-        for filename in glob.glob(os.path.join(libs_path, '*openblas*dll')):
-            # NOTE: would it change behavior to load ALL
-            # DLLs at this path vs. the name restriction?
-            WinDLL(os.path.abspath(filename))
-            DLL_filenames.append(filename)
-    if len(DLL_filenames) > 1:
-        import warnings
-        warnings.warn("loaded more than 1 DLL from .libs:\n%s" %
-                      "\n".join(DLL_filenames),
-                      stacklevel=1)
-
 # disables OpenBLAS affinity setting of the main thread that limits
 # python threads or processes to one core
 env_added = []
diff --git a/tools/openblas_support.py b/tools/openblas_support.py
new file mode 100644
--- /dev/null
+++ b/tools/openblas_support.py
@@ -0,0 +1,42 @@
+import os
+import textwrap
+
+def make_init(dirname):
+    '''
+    Create a _distributor_init.py file for OpenBlas
+    '''
+    with open(os.path.join(dirname, '_distributor_init.py'), 'wt') as fid:
+        fid.write(textwrap.dedent("""
+            '''
+            Helper to preload windows dlls to prevent dll not found errors.
+            Once a DLL is preloaded, its namespace is made available to any
+            subsequent DLL. This file originated in the numpy-wheels repo,
+            and is created as part of the scripts that build the wheel.
+            '''
+            import os
+            from ctypes import WinDLL
+            import glob
+            if os.name == 'nt':
+                # convention for storing / loading the DLL from
+                # numpy/.libs/, if present
+                try:
+                    basedir = os.path.dirname(__file__)
+                except:
+                    pass
+                else:
+                    libs_dir = os.path.abspath(os.path.join(basedir, '.libs'))
+                    DLL_filenames = []
+                    if os.path.isdir(libs_dir):
+                        for filename in glob.glob(os.path.join(libs_dir,
+                                                             '*openblas*dll')):
+                            # NOTE: would it change behavior to load ALL
+                            # DLLs at this path vs. the name restriction?
+                            WinDLL(os.path.abspath(filename))
+                            DLL_filenames.append(filename)
+                if len(DLL_filenames) > 1:
+                    import warnings
+                    warnings.warn("loaded more than 1 DLL from .libs:\\n%s" %
+                              "\\n".join(DLL_filenames),
+                              stacklevel=1)
+    """))
+


Test patch: