pw_build: Fine tune python_runner's PYTHONPATH logic

If no __init__.py exists for a given python_dep, don't add it to the
PYTHONPATH.

Bug: b/235615763
Change-Id: Idb18114ca48d39bfbfe9d0f110accb3f82cdc0b4
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/97790
Commit-Queue: Anthony DiGirolamo <tonymd@google.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
diff --git a/pw_build/py/pw_build/python_runner.py b/pw_build/py/pw_build/python_runner.py
index 451b822..82a9b80 100755
--- a/pw_build/py/pw_build/python_runner.py
+++ b/pw_build/py/pw_build/python_runner.py
@@ -565,7 +565,12 @@
                 raise MissingPythonDependency(
                     'Unable to find top level source dir for the Python '
                     f'package "{pkg}"')
+            # Don't add this dir to the PYTHONPATH if no __init__.py exists.
+            init_py_files = top_level_source_dir.parent.glob('*/__init__.py')
+            if not any(init_py_files):
+                continue
             python_paths_list.append(top_level_source_dir.parent.resolve())
+
         # Sort the PYTHONPATH list, it will be in a different order each build.
         python_paths_list = sorted(python_paths_list)
 
diff --git a/pw_protobuf_compiler/proto.gni b/pw_protobuf_compiler/proto.gni
index 9499a07..f91d4c5 100644
--- a/pw_protobuf_compiler/proto.gni
+++ b/pw_protobuf_compiler/proto.gni
@@ -374,6 +374,13 @@
       generate_setup = {
         metadata = {
           name = invoker._package_dir
+
+          # The package name should match where the __init__.py lives. If
+          # module_as_package is specified use that for the Python package name.
+          if (defined(invoker.module_as_package) &&
+              invoker.module_as_package != "") {
+            name = invoker.module_as_package
+          }
           version =
               "0.0.1"  # TODO(hepler): Need to be able to set this verison.
         }