Skip to content

fix(python314): replace deprecated dependency pkg_resources

1. Summary

I replaced deprecated pkg_recources dependency in favor of importlib module of the Python Standard Library. After this change flake8-use-pathlib should successfully work on Python 3.14.

2. Argumentation of changes

The attention on the official Setuptools documentation:

Use of pkg_resources is deprecated in favor of importlib.resources, importlib.metadata and their backports (importlib_resources, importlib_metadata). Some useful APIs are also provided by packaging (e.g. requirements and version parsing). Users should refrain from new usage of pkg_resources and should work to port to importlib-based solutions.

I replaced getting package version via pkg_resources to importlib.metadata.version() as described on the official importlib documentation.

3. Behavior

3.1. Before changes

flake8-use-pathlib doesn’t work for me on Python 3.14:

D:\SashaDebugging\KiraFlake8Debugging>flake8 --version
Traceback (most recent call last):
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8\plugins\finder.py", line 291, in _load_plugin
    obj = plugin.entry_point.load()
  File "C:\Python314\Lib\importlib\metadata\__init__.py", line 179, in load
    module = import_module(match.group('module'))
  File "C:\Python314\Lib\importlib\__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1398, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1371, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1342, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 938, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 762, in exec_module
  File "<frozen importlib._bootstrap>", line 491, in _call_with_frames_removed
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8_use_pathlib\__init__.py", line 1, in <module>
    from .checker import PathlibChecker
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8_use_pathlib\checker.py", line 6, in <module>
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Scripts\flake8.EXE\__main__.py", line 7, in <module>
    sys.exit(main())
             ~~~~^^
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8\main\cli.py", line 23, in main
    app.run(argv)
    ~~~~~~~^^^^^^
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8\main\application.py", line 198, in run
    self._run(argv)
    ~~~~~~~~~^^^^^^
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8\main\application.py", line 186, in _run
    self.initialize(argv)
    ~~~~~~~~~~~~~~~^^^^^^
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8\main\application.py", line 165, in initialize
    self.plugins, self.options = parse_args(argv)
                                 ~~~~~~~~~~^^^^^^
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8\options\parse_args.py", line 42, in parse_args
    plugins = finder.load_plugins(raw_plugins, plugin_opts)
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8\plugins\finder.py", line 365, in load_plugins
    return _classify_plugins(_import_plugins(plugins, opts), opts)
                             ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8\plugins\finder.py", line 307, in _import_plugins
    return [_load_plugin(p) for p in plugins]
            ~~~~~~~~~~~~^^^
  File "D:\SashaDebugging\KiraFlake8Debugging\.venv\Lib\site-packages\flake8\plugins\finder.py", line 293, in _load_plugin
    raise FailedToLoadPlugin(plugin.package, e)
flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "flake8-use-pathlib" due to No module named 'pkg_resources'.

3.2. After changes

flake8-use-pathlib successfully works for me.

flake8 --version
7.3.0 (flake8-use-pathlib: 0.3.0, mccabe: 0.7.0, pycodestyle: 2.14.0, pyflakes: 3.4.0) CPython 3.14.0 on Windows

flake8 KiraTest.py
KiraTest.py:7:1: PL100 os.path.abspath("foo") should be replaced by foo_path.resolve()

4. Testing environment

  1. Windows 11 [Version 10.0.22621.3085]
  2. Python 3.14.0
  3. flake8 7.3.0
  4. flake8-use-pathlib 0.3.0

Thanks.

Merge request reports

Loading