diff --git a/kayobe/tests/unit/test_utils.py b/kayobe/tests/unit/test_utils.py
index 85e9833744205657422487f90ed139a7995e1901..e1b4e2b0281fb9f6416e34ee009c97a77f4ec785 100644
--- a/kayobe/tests/unit/test_utils.py
+++ b/kayobe/tests/unit/test_utils.py
@@ -179,6 +179,13 @@ key2: value2
         result = utils._detect_install_prefix(path)
         self.assertEqual(expected, os.path.normpath(result))
 
+    def test_detect_install_prefix_multiple_lib_components(self):
+        tmp_path = os.path.realpath('/tmp')
+        path = "%s/lib/test/local/lib/python3.6/dist-packages" % tmp_path
+        expected = os.path.normpath("%s/lib/test/local/" % tmp_path)
+        result = utils._detect_install_prefix(path)
+        self.assertEqual(expected, os.path.normpath(result))
+
     def test_intersect_limits_no_arg_no_cli(self):
         result = utils.intersect_limits(None, None)
         self.assertEqual("", result)
diff --git a/kayobe/utils.py b/kayobe/utils.py
index 2ded367c4b2227af5c8dde45bf27cba1f1766d71..727fd783a6acfd080fa7172d5075dd4a2b26d84f 100644
--- a/kayobe/utils.py
+++ b/kayobe/utils.py
@@ -14,7 +14,6 @@
 
 import base64
 import glob
-import itertools
 import logging
 import os
 import shutil
@@ -40,13 +39,11 @@ def _detect_install_prefix(path):
     script_path = os.path.realpath(path)
     script_path = os.path.normpath(script_path)
     components = script_path.split(os.sep)
-    # use heuristic: anything before 'lib' in path is the prefix
+    # use heuristic: anything before the last 'lib' in path is the prefix
     if 'lib' not in components:
         return None
-    prefix = itertools.takewhile(
-        lambda x: x != "lib",
-        components
-    )
+    last_lib = len(components) - 1 - components[::-1].index('lib')
+    prefix = components[:last_lib]
     prefix_path = os.sep.join(prefix)
     return prefix_path
 
diff --git a/releasenotes/notes/install-prefix-multiple-lib-9288e8c11da3c0bc.yaml b/releasenotes/notes/install-prefix-multiple-lib-9288e8c11da3c0bc.yaml
new file mode 100644
index 0000000000000000000000000000000000000000..4043b6deab0977c3541798b51c53bba167d6a284
--- /dev/null
+++ b/releasenotes/notes/install-prefix-multiple-lib-9288e8c11da3c0bc.yaml
@@ -0,0 +1,6 @@
+---
+fixes:
+  - |
+    Fixes a failure to detect the Kayobe installation prefix when ``lib`` is
+    present multiple times in the installation path. See `story 2009721
+    <https://storyboard.openstack.org/#!/story/2009721>`__ for details.