From c4ef12331a8ad674c52596bb8de9d8e8ab28c586 Mon Sep 17 00:00:00 2001
From: Christoph Gohlke <cjgohlke@gmail.com>
Date: Wed, 4 Dec 2019 12:14:14 -0800
Subject: [PATCH] Fix slow import of pyopencl

---
 pyopencl/__init__.py | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/pyopencl/__init__.py b/pyopencl/__init__.py
index 683382d6..bda4800b 100644
--- a/pyopencl/__init__.py
+++ b/pyopencl/__init__.py
@@ -255,23 +255,19 @@ def compiler_output(text):
 # {{{ find pyopencl shipped source code
 
 def _find_pyopencl_include_path():
-    from pkg_resources import Requirement, resource_filename, DistributionNotFound
+    from os.path import join, abspath, dirname, exists
     try:
+        # Try to find the include path in the same directory as this file
+        include_path = join(abspath(dirname(__file__)), "cl")
+        if not exists(include_path):
+            raise IOError()
+    except Exception:
         # Try to find the resource with pkg_resources (the recommended
-        # setuptools approach)
+        # setuptools approach). This is very slow.
+        from pkg_resources import (Requirement, resource_filename,
+                                   DistributionNotFound)
         include_path = resource_filename(
                 Requirement.parse("pyopencl"), "pyopencl/cl")
-    except DistributionNotFound:
-        # If pkg_resources can't find it (e.g. if the module is part of a
-        # frozen application), try to find the include path in the same
-        # directory as this file
-        from os.path import join, abspath, dirname, exists
-
-        include_path = join(abspath(dirname(__file__)), "cl")
-        # If that doesn't exist, just re-raise the exception caught from
-        # resource_filename.
-        if not exists(include_path):
-            raise
 
     # Quote the path if it contains a space and is not quoted already.
     # See https://github.com/inducer/pyopencl/issues/250 for discussion.
-- 
GitLab