Skip to content

google.cloud.alloydb PEP 420 namespace issue #13871

@jcrobak

Description

@jcrobak

Determine this is the right repository

  • I determined this is the correct repository in which to report this bug.

Summary of the issue

Context
I am trying to use google-cloud-alloydb and alloydb-python-connector in the same Python environment.

Expected Behavior:
I expected to be able to import from google.cloud.alloydb.connector after installing both packages.

Actual Behavior:
I get a ModuleNotFoundError when trying to import google.cloud.alloydb.connector, because the google-cloud-alloydb package installs a concrete __init__.py file in google/cloud/alloydb/, which breaks namespace package resolution (PEP 420)

API client name and version

google-cloud-alloydb-connector 1.8.0, google-cloud-alloydb 0.4.5

Reproduction steps: code

file: test.py

from google.cloud.alloydb.connector import Connector

print("Connector loaded successfully")

Reproduction steps: supporting files

Here is a self-contained repro shell script that shows the issue with namespace packages:

file: repro.sh

#!/bin/bash
set -euo pipefail

WORKDIR=$(mktemp -d)
echo "Working directory: $WORKDIR"

# Create two isolated package install dirs
PKG_CONNECTOR="$WORKDIR/pkg_alloydb_connector"
PKG_CLOUD="$WORKDIR/pkg_cloud_alloydb"

mkdir -p "$PKG_CONNECTOR" "$PKG_CLOUD"

python3 -m venv "$WORKDIR/venv"
source "$WORKDIR/venv/bin/activate"

pip3 install --upgrade pip setuptools wheel

# Install each package into a separate directory
pip3 install --target="$PKG_CONNECTOR" google-cloud-alloydb-connector[pg8000]
pip3 install --target="$PKG_CLOUD" google-cloud-alloydb

# Show resulting google.cloud tree
echo
echo "Installed paths:"
find "$WORKDIR" -path '*/google/cloud/*' -type d

echo
echo "Attempting import..."
PYTHONPATH="$PKG_CLOUD:$PKG_CONNECTOR" python3 -c "from google.cloud.alloydb.connector import Connector" || {
  echo
  echo "❌ Import failed as expected due to concrete __init__.py blocking namespace package resolution"
  exit 1
}

Note that if you reverse the order of the $PYTHONPATH entries, the import does work.

Reproduction steps: actual results

Traceback (most recent call last):
  File "/tmp/pex-repro/test.py", line 1, in <module>
    from google.cloud.alloydb.connector import Connector
ModuleNotFoundError: No module named 'google.cloud.alloydb.connector'

Reproduction steps: expected results

Connector loaded successfully

OS & version + platform

Ubuntu 22.04 on x86_64

Python environment

Python 3.11.8

Python dependencies

No response

Additional context

This issue only manifests in isolated environments like PEX or when manually simulating PEX-style import resolution. The problem arises because google/cloud/alloydb/__init__.py is a concrete file (i.e., not empty or omitted), which causes Python to treat google.cloud.alloydb as a non-namespace package.

As a result, google.cloud.alloydb.connector, becomes invisible to the import system.

Why this matters

This breaks compatibility for any downstream environment or tool that expects namespace packages to behave per PEP 420. In particular, PEX environments fail to import google.cloud.alloydb.connector, even when it's installed.

Suggested Fix

Please remove the google/cloud/alloydb/__init__.py file entirely from the google-cloud-alloydb distribution. This will allow Python to treat the directory as a namespace package, making it compatible with alloydb-python-connector.

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions