Francis Smart
Francis Smart

Reputation: 4065

Implement pkg_resources.resource_filename in setuptools

I am working to incorporate the symspellpy package for spell checking and correcting large amounts of data. However, the package suggests using pkg_resources.resource_filename, which is no longer supported. Can you please provide guidance on how to access the necessary resources using the currently preferred method?

dictionary_path = pkg_resources.resource_filename("symspellpy", "frequency_dictionary_en_82_765.txt")
bigram_path = pkg_resources.resource_filename("symspellpy", "frequency_bigramdictionary_en_243_342.txt")

Upvotes: 0

Views: 992

Answers (1)

neutrinoceros
neutrinoceros

Reputation: 367

The replacement is the importlib_resources.files function. It is integrated in standard library from Python 3.9, as importlib.resources.files

If you just need to support Python 3.9 or newer, it's straightforward

import importlib.resources

importlib.resources.files(...)

Otherwise, if you want to support Python 3.8 and older, here's how you do it:

  1. add importlib_resources>=1.3; python_version < '3.9' to your dependencies (requirements.txt, setup.cfg, setup.py or pyproject.toml, depending how the project is organised)
  2. In your code, adapt as
import sys

if sys.version_info >= (3, 9):
    import importlib.resources as importlib_resources
else:
    import importlib_resources

importlib_resources.files(...)

See https://importlib-resources.readthedocs.io/en/latest/migration.html

Upvotes: 0

Related Questions