diff --git a/numba/core/typeinfer.py b/numba/core/typeinfer.py
index 59d041b9c..059019a18 100644
--- a/numba/core/typeinfer.py
+++ b/numba/core/typeinfer.py
@@ -1468,13 +1468,25 @@ http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-has-an-u
                 # as a global variable
                 typ = types.Dispatcher(_temporary_dispatcher_map[gvar.name])
             else:
+                from numba.misc import special
+
                 nm = gvar.name
-                msg = _termcolor.errmsg("Untyped global name '%s':" % nm)
+                # check if the problem is actually a name error
+                func_glbls = self.func_id.func.__globals__
+                if (nm not in func_glbls.keys() and
+                        nm not in special.__all__ and
+                        nm not in __builtins__.keys()):
+                    errstr = "NameError: name '%s' is not defined"
+                    msg = _termcolor.errmsg(errstr % nm)
+                    e.patch_message(msg)
+                    raise
+                else:
+                    msg = _termcolor.errmsg("Untyped global name '%s':" % nm)
                 msg += " %s"  # interps the actual error
 
                 # if the untyped global is a numba internal function then add
                 # to the error message asking if it's been imported.
-                from numba.misc import special
+
                 if nm in special.__all__:
                     tmp = ("\n'%s' looks like a Numba internal function, has "
                            "it been imported (i.e. 'from numba import %s')?\n" %

Improved error message for non existing variable
Attempting to use a variable that has not been defined returns an error message that can be confusing for someone new to Numba.

```python
@njit
def foo(x):
    a = monkey
    return a

foo(np.ones(2))

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Untyped global name 'monkey': cannot determine Numba type of <class 'numba.core.ir.UndefinedType'>
```

while Python would return a much clearer `NameError: name 'monkey' is not defined`.
diff --git a/numba/tests/test_errorhandling.py b/numba/tests/test_errorhandling.py
index 1a1478d26..3cc306e2b 100644
--- a/numba/tests/test_errorhandling.py
+++ b/numba/tests/test_errorhandling.py
@@ -171,6 +171,16 @@ class TestMiscErrorHandling(unittest.TestCase):
                 dec(foo)()
             self.assertIn(expected, str(raises.exception))
 
+    def test_handling_undefined_variable(self):
+        @njit
+        def foo():
+            return a
+
+        expected = "NameError: name 'a' is not defined"
+
+        with self.assertRaises(errors.TypingError) as raises:
+            foo()
+        self.assertIn(expected, str(raises.exception))
 
 class TestConstantInferenceErrorHandling(unittest.TestCase):
 
diff --git a/numba/tests/test_typingerror.py b/numba/tests/test_typingerror.py
index 0cbb4a9ed..ce3bcc6dd 100644
--- a/numba/tests/test_typingerror.py
+++ b/numba/tests/test_typingerror.py
@@ -82,7 +82,7 @@ class TestTypingError(unittest.TestCase):
         # This used to print "'object' object has no attribute 'int32'"
         with self.assertRaises(TypingError) as raises:
             compile_isolated(unknown_module, ())
-        self.assertIn("Untyped global name 'numpyz'", str(raises.exception))
+        self.assertIn("name 'numpyz' is not defined", str(raises.exception))
 
     def test_issue_868(self):
         '''

pip install -e .
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y gcc g++
3.7
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
certifi @ file:///croot/certifi_1671487769961/work/certifi
exceptiongroup==1.2.2
importlib-metadata==6.7.0
iniconfig==2.0.0
llvmlite==0.33.0
-e git+https://github.com/numba/numba.git@92df8df57dd31a3da51d3b0cf94a4ec2b591f71a#egg=numba
numpy==1.21.6
packaging==24.0
pluggy==1.2.0
pytest==7.4.4
tomli==2.0.1
typing_extensions==4.7.1
zipp==3.15.0

name: numba
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - argparse==1.4.0
      - exceptiongroup==1.2.2
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - llvmlite==0.33.0
      - numpy==1.21.6
      - packaging==24.0
      - pluggy==1.2.0
      - pytest==7.4.4
      - tomli==2.0.1
      - typing-extensions==4.7.1
      - zipp==3.15.0
prefix: /opt/conda/envs/numba

numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_handling_undefined_variable numba/tests/test_typingerror.py::TestTypingError::test_unknown_module
numba/tests/test_errorhandling.py::TestErrorHandlingBeforeLowering::test_unsupported_make_function_return_inner_func numba/tests/test_errorhandling.py::TestUnsupportedReporting::test_unsupported_numpy_function numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_commented_func_definition_is_not_a_definition numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_handling_forgotten_numba_internal_import numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_handling_of_write_to_reflected_global numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_handling_of_write_to_typed_dict_global numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_handling_unsupported_generator_expression numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_use_of_exception_for_flow_control numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_use_of_ir_unknown_loc numba/tests/test_errorhandling.py::TestConstantInferenceErrorHandling::test_basic_error numba/tests/test_errorhandling.py::TestErrorMessages::test_error_function_source_is_correct numba/tests/test_errorhandling.py::TestErrorMessages::test_error_in_intrinsic numba/tests/test_errorhandling.py::TestErrorMessages::test_no_match_error numba/tests/test_errorhandling.py::TestErrorMessages::test_specific_error numba/tests/test_typingerror.py::TestTypingError::test_array_setitem_invalid_cast numba/tests/test_typingerror.py::TestTypingError::test_bad_hypot_usage numba/tests/test_typingerror.py::TestTypingError::test_imprecise_list numba/tests/test_typingerror.py::TestTypingError::test_issue_868 numba/tests/test_typingerror.py::TestTypingError::test_return_type_unification numba/tests/test_typingerror.py::TestTypingError::test_template_rejection_error_message_cascade numba/tests/test_typingerror.py::TestTypingError::test_unknown_attrs numba/tests/test_typingerror.py::TestTypingError::test_unknown_function numba/tests/test_typingerror.py::TestTypingError::test_using_imprecise_list numba/tests/test_typingerror.py::TestArgumentTypingError::test_unsupported_array_dtype numba/tests/test_typingerror.py::TestArgumentTypingError::test_unsupported_type numba/tests/test_typingerror.py::TestCallError::test_readonly_array
numba/core/typeinfer.py
numba/core/typeinfer.py