Skip to content

Import fails on Python 3.12 in virtualenv, pkg_resources not installed by default #275

@jwhitlock

Description

@jwhitlock

If you are running a virtualenv with Python 3.11 with warnings enabled, you get a deprecation warning. On macOS Sonoma 14.5, with pyenv:

pyenv install 3.11
mkdir -p /tmp/workspace
cd /tmp/workspace
python3.11 -m venv .py311
.py311/bin/python3.11 -m pip install google-cloud-sqlcommenter==2.0.0
.py311/bin/python3.11 -Wall -c "import google.cloud.sqlcommenter"
/private/tmp/workspace/.py311/lib/python3.11/site-packages/google/__init__.py:17: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  __import__('pkg_resources').declare_namespace(__name__)
/private/tmp/workspace/.py311/lib/python3.11/site-packages/google/__init__.py:17: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  __import__('pkg_resources').declare_namespace(__name__)

pkg_resources is no longer installed in a virtualenv in Python 3.12, so you get an error:

pyenv install 3.12
mkdir -p /tmp/workspace
cd /tmp/workspace
python3.12 -m venv .py312
.py312/bin/python3.12 -m pip install google-cloud-sqlcommenter==2.0.0
.py312/bin/python3.12 -Wall -c "import google.cloud.sqlcommenter"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/private/tmp/workspace/.py312/lib/python3.12/site-packages/google/__init__.py", line 17, in <module>
    __import__('pkg_resources').declare_namespace(__name__)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'pkg_resources'

The fix is to install setuptools, then you just get all the warnings again:

.py312/bin/python3.12 -m pip install setuptools
.py312/bin/python3.12 -Wall -c "import google.cloud.sqlcommenter"
/private/tmp/workspace/.py312/lib/python3.12/site-packages/google/__init__.py:17: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  __import__('pkg_resources').declare_namespace(__name__)
/private/tmp/workspace/.py312/lib/python3.12/site-packages/google/__init__.py:17: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('google')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  __import__('pkg_resources').declare_namespace(__name__)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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