Skip to content

Commit

Permalink
Replace deprecated pkg_resources with stdlib
Browse files Browse the repository at this point in the history
`pkg_resources` has been deprecated by `setuptools` for quite a while:
https://setuptools.pypa.io/en/latest/pkg_resources.html

It's got some bugs/warts:

* pypa/setuptools#2531
* https://discuss.python.org/t/will-setuptools-remove-pkg-resource-module-in-the-future/27182

So switch to using `importlib` functions which are part of the Python
standard library as of `3.8`.

This is less error-prone, and also removes the need for `setuptools` to
be installed in order for this panel to work.

I realize we technically still support `3.7`, but I thought it was fine
to change this particular panel to require `3.8`, as `3.7` support is
best effort given that it's now EOL'd by the core Python team.

I also removed the relative path location for specific libraries as it
was simply blank for me on Python 3.12... I think showing the location
of the site packages directory should suffice. If someone later wants to
build this out further, they're more than welcome to.

Note that `importlib.metadata.distributions()` does have an outstanding
issue that it reports a local editable install twice, but they plan to
eventually fix that:
* pypa/setuptools#4170
  • Loading branch information
jeffwidman committed Apr 13, 2024
1 parent 7f3deff commit 3ea2d48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
19 changes: 4 additions & 15 deletions src/flask_debugtoolbar/panels/versions.py
Expand Up @@ -17,16 +17,6 @@
_ = lambda x: x


def relpath(location, python_lib):
location = os.path.normpath(location)
relative = os.path.relpath(location, python_lib)
if relative == os.path.curdir:
return ''
elif relative.startswith(os.path.pardir):
return location
return relative


class VersionDebugPanel(DebugPanel):
"""
Panel that displays the Flask version.
Expand All @@ -48,15 +38,14 @@ def title(self):

def content(self):
try:
import pkg_resources
import importlib.metadata
except ImportError:
packages = []
else:
packages = sorted(pkg_resources.working_set,
key=lambda p: p.project_name.lower())
packages_metadata = [p.metadata for p in importlib.metadata.distributions()]
packages = sorted(packages_metadata, key=lambda p: p['Name'].lower())

return self.render('panels/versions.html', {
'packages': packages,
'python_lib': os.path.normpath(get_path('platlib')),
'relpath': relpath,
'python_lib_dir': os.path.normpath(get_path('platlib')),
})
18 changes: 10 additions & 8 deletions src/flask_debugtoolbar/templates/panels/versions.html
@@ -1,32 +1,34 @@
<h4>Installed Packages</h4>

<p>
Installation paths relative to:
Current Site Packages Directory:
</p>
<pre>
{{ python_lib }}
{{ python_lib_dir }}
</pre>

<table>
<thead>
<tr>
<th>Package</th>
<th>Version</th>
<th>Installed Path</th>
<th>Homepage</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
{% for package in packages %}
<tr class="{{ loop.cycle('flDebugOdd', 'flDebugEven') }}">
<td>{{ package.project_name }}</td>
<td>{{ package.version }}</td>
<td>{{ relpath(package.location, python_lib) }}</td>
<td>{{ package.get('Name') }}</td>
<td>{{ package.get('Version') }}</td>
<td>{{ package.get('Home-page') }}</td>
<td>{{ package.get('Summary') }}</td>
</tr>
{% else %}
<tr>
<td>setuptools</td>
<td>Python 3.8</td>
<td>NOT INSTALLED</td>
<td>Install setuptools to display installed packages and version information</td>
<td>This panel requires Python >= 3.8 in order to display installed packages and version information.</td>
</tr>
{% endfor %}
</tbody>
Expand Down

0 comments on commit 3ea2d48

Please sign in to comment.