Skip to content

Commit 14f5b96

Browse files
committed
Migrate away from the deprecated pkgutil.find_loader()
In the newly released Python 3.12, the `pkgutil.find_loader()` API was deprecated: https://docs.python.org/3.12/library/pkgutil.html#pkgutil.find_loader It's been replaced by `importlib.util.find_spec` which is supported since Python 3.4 (which is fine for us to use now, but wasn't an option back when `is_module_available()` was written, since at that time the buildpack still had to support Python 2.7): https://docs.python.org/3.12/library/importlib.html#importlib.util.find_spec This API was used by the buildpack to determine whether certain packages (like `django` or `nltk`) are installed, and so resulted in deprecation warnings being output in the build log: ``` <string>:1: DeprecationWarning: 'pkgutil.find_loader' is deprecated and slated for removal in Python 3.14; use importlib.util.find_spec() instead ``` These deprecation warnings also prevented the output from `bin/release` being parsed correctly for Django apps, resulting in: ``` failed to read buildpack metadata: (<unknown>): mapping values are not allowed in this context at line 1 column 31 ``` These can be seen in the Heroku CI tests and Review App build logs of this PR which upgrades the Python getting started guide app to Python 3.12: heroku/python-getting-started#209 https://dashboard.heroku.com/pipelines/4826c212-2bb2-4882-a147-4d0501eee7a4/tests/190 https://dashboard.heroku.com/apps/getting-star-edmorley-p-biivve/activity/builds/a6feca96-d451-4f01-b66d-c5d2185d5107 GUS-W-14230170.
1 parent 6c49dd8 commit 14f5b96

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Fixed `pkgutil.find_loader` deprecation warning when using Python 3.12. ([#1493](https://github.com/heroku/heroku-buildpack-python/pull/1493))
56

67
## v236 (2023-10-02)
78

bin/utils

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,10 @@ measure-size() {
5858
echo "$(du -s .heroku/python 2>/dev/null || echo 0) | awk '{print $1}')"
5959
}
6060

61+
# Returns 0 if the specified module exists, otherwise returns 1.
6162
is_module_available() {
62-
# Returns 0 if the specified module exists, otherwise returns 1.
63-
# Uses pkgutil rather than pkg_resources or pip's CLI, since pkgutil exists
64-
# in the stdlib, and doesn't depend on the choice of package manager.
6563
local module_name="${1}"
66-
python -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('${module_name}') else 1)"
64+
python -c "import sys, importlib.util; sys.exit(0 if importlib.util.find_spec('${module_name}') else 1)"
6765
}
6866

6967
# The requirement versions are effectively buildpack constants, however, we want

0 commit comments

Comments
 (0)