Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jwhitlock opened this issue May 23, 2024 · 1 comment

Comments

@jwhitlock
Copy link

jwhitlock commented May 23, 2024

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 jwhitlock changed the title Import fails on Python 3.12, pkg_resources removed Import fails on Python 3.12 in virtualenv, pkg_resources not installed by default May 23, 2024
@jwhitlock
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant