google / sqlcommenter

Apache License 2.0
584 stars 78 forks source link

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

Open jwhitlock opened 1 month ago

jwhitlock commented 1 month ago

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__)
jwhitlock commented 1 month ago

The error is because pkg_resources is no longer installed by default in a virtualenv. Installing setuptools fixes the issue, but can cause problems if you are not in a virtualenv. I've updated the issue to be more precise.

The issue would be fixed by no longer declaring a namespace, as suggested in the links in the DeprecationWarning.