Clean up invocation of nanopb_generator.py in test cases.
diff --git a/tests/generator_relative_paths/SConscript b/tests/generator_relative_paths/SConscript
index d17bec7..a7c727a 100644
--- a/tests/generator_relative_paths/SConscript
+++ b/tests/generator_relative_paths/SConscript
@@ -23,22 +23,15 @@
 
 # As of 0.4.2, SCons rules still go through protoc that handles paths correctly
 # by itself. To test direct nanopb_generator usage we invoke it manually here.
-
-generator_cmd = os.path.join(env['NANOPB'], 'generator-bin', 'nanopb_generator' + env['PROGSUFFIX'])
-if os.path.exists(generator_cmd):
-    generator_cmd = env['ESCAPE'](generator_cmd)
-else:
-    generator_cmd = env['ESCAPE'](sys.executable) + " " + env['ESCAPE'](os.path.join(env['NANOPB'], 'generator', 'nanopb_generator.py'))
-
 env.Command(["build/protobuf/any.pb.h", "build/simple.pb.h", "build/protobuf/any.pb.c", "build/simple.pb.c",],
             ["proto/protobuf/any.proto", "proto/simple.proto"],
 [
     Delete("build/generator_relative_paths/build"),
     Mkdir("build/generator_relative_paths/build"),
-    generator_cmd + " -Dbuild/generator_relative_paths/build -Ibuild/generator_relative_paths/proto $SOURCES"
+    env['NANOPB_GENERATOR'] + " -Dbuild/generator_relative_paths/build -Ibuild/generator_relative_paths/proto $SOURCES"
 ])
 
 env.Match("simple_pb_h_ok", ["build/simple.pb.h", "simple.expected"])
 env.Match("simple_pb_c_ok", ["build/simple.pb.c", "simple.expected"])
 env.Match("any_pb_h_ok", ["build/protobuf/any.pb.h", "any.expected"])
-env.Match("any_pb_c_ok", ["build/protobuf/any.pb.c", "any.expected"])
\ No newline at end of file
+env.Match("any_pb_c_ok", ["build/protobuf/any.pb.c", "any.expected"])
diff --git a/tests/regression/issue_494/SConscript b/tests/regression/issue_494/SConscript
index 07709ea..84f4130 100644
--- a/tests/regression/issue_494/SConscript
+++ b/tests/regression/issue_494/SConscript
@@ -7,15 +7,8 @@
 # The build rules here are a bit tricky to make the normal dependency
 # resolution intentionally fail. This causes the generator to use the fallback
 # define which had the problem with C++.
-
-generator_cmd = os.path.join(env['NANOPB'], 'generator-bin', 'nanopb_generator' + env['PROGSUFFIX'])
-if os.path.exists(generator_cmd):
-    generator_cmd = env['ESCAPE'](generator_cmd)
-else:
-    generator_cmd = env['ESCAPE'](sys.executable) + " " + env['ESCAPE'](os.path.join(env['NANOPB'], 'generator', 'nanopb_generator.py'))
-
 env.Command("oneof.pb", "oneof.proto", "$PROTOC $PROTOCFLAGS -Ibuild/regression/issue_494 -o$TARGETS $SOURCES")
-env.Command(["oneof.pb.c", "oneof.pb.h"], "oneof.pb", generator_cmd + " -Dbuild/regression/issue_494 $SOURCES")
+env.Command(["oneof.pb.c", "oneof.pb.h"], "oneof.pb", env['NANOPB_GENERATOR'] + " -Dbuild/regression/issue_494 $SOURCES")
 env.NanopbProto("submessage.proto")
 env.Depends("oneof.pb", "submessage.proto")
 
diff --git a/tests/site_scons/site_tools/nanopb.py b/tests/site_scons/site_tools/nanopb.py
index ffad59d..f0f386c 100644
--- a/tests/site_scons/site_tools/nanopb.py
+++ b/tests/site_scons/site_tools/nanopb.py
@@ -53,6 +53,31 @@
     raise SCons.Errors.StopError(NanopbWarning,
         "Could not find the nanopb root directory")
 
+def _detect_python(env):
+    '''Find Python executable to use.'''
+    if env.has_key('PYTHON'):
+        return env['PYTHON']
+
+    p = env.WhereIs('python3')
+    if p:
+        return env['ESCAPE'](p)
+
+    p = env.WhereIs('py.exe')
+    if p:
+        return env['ESCAPE'](p) + " -3"
+
+    return env['ESCAPE'](sys.executable)
+
+def _detect_nanopb_generator(env):
+    '''Return command for running nanopb_generator.py'''
+    generator_cmd = os.path.join(env['NANOPB'], 'generator-bin', 'nanopb_generator' + env['PROGSUFFIX'])
+    if os.path.exists(generator_cmd):
+        # Binary package
+        return env['ESCAPE'](generator_cmd)
+    else:
+        # Source package
+        return env['PYTHON'] + " " + env['ESCAPE'](os.path.join(env['NANOPB'], 'generator', 'nanopb_generator.py'))
+
 def _detect_protoc(env):
     '''Find the path to the protoc compiler.'''
     if env.has_key('PROTOC'):
@@ -148,6 +173,8 @@
     env['NANOPB'] = _detect_nanopb(env)
     env['PROTOC'] = _detect_protoc(env)
     env['PROTOCFLAGS'] = _detect_protocflags(env)
+    env['PYTHON'] = _detect_python(env)
+    env['NANOPB_GENERATOR'] = _detect_nanopb_generator(env)
     env.SetDefault(NANOPBFLAGS = '')
 
     env.SetDefault(PROTOCPATH = [".", os.path.join(env['NANOPB'], 'generator', 'proto')])