diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index 046bf4b8b..7ee0479aa 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -379,14 +379,15 @@ def better_compile(text, file, realfile, mode = "exec", lineno = 0):
         error = []
         # split the text into lines again
         body = text.split('\n')
-        error.append("Error in compiling python function in %s, line %s:\n" % (realfile, e.lineno))
-        if hasattr(e, "lineno"):
+        if getattr(e, "lineno", None) is not None:
+            error.append("Error in compiling python function in %s, line %s:\n" % (realfile, e.lineno))
             error.append("The code lines resulting in this error were:")
             # e.lineno: line's position in reaflile
             # lineno: function name's "position -1" in realfile
             # e.lineno - lineno: line's relative position in function
             error.extend(_print_trace(body, e.lineno - lineno))
         else:
+            error.append("Error in compiling python function in %s:\n" % realfile)
             error.append("The function causing this error was:")
             for line in body:
                 error.append(line)
