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

Fix resource file access to support zipped package environments #324

Closed

Conversation

novikovaTanya
Copy link
Contributor

@novikovaTanya novikovaTanya commented Oct 1, 2024

Description

This PR addresses an issue where the biip package fails to access resource files when deployed in environments where the package is zipped, such as in Snowflake UDFs or other systems that package Python modules into zip files. The error encountered is:

NotADirectoryError: [Errno 20] Not a directory: '/path/to/biip.zip/biip/gs1/_application_identifiers.json'

Problem

The current implementation uses pathlib and __file__ to construct a file path to _application_identifiers.json:

_GS1_APPLICATION_IDENTIFIERS_FILE = (
    pathlib.Path(__file__).parent / "_application_identifiers.json"
)

This approach assumes that the package files are accessible via the file system, which is not the case when the package is zipped. In such cases, attempting to access files using file paths results in a NotADirectoryError.

Solution

Modify the code to use importlib.resources for accessing the resource file. This module is designed for reading resources bundled within Python packages, regardless of whether they are stored in the file system or in a zip archive.

Changes:

  • Replace the use of pathlib and __file__ with importlib.resources.
  • Update the code to load the JSON file using importlib.resources.open_text().

Benefits

  • Compatibility: Ensures that biip works correctly in environments where packages are zipped.

  • Portability: Aligns with Python's recommended practices for resource file access.

  • No External Dependencies: Uses the standard library available in Python 3.7 and above.

Reference Documentation

Conclusion

This change enhances the portability and robustness of the biip package by ensuring it can access its resource files regardless of the packaging format. It aligns with best practices for resource management in Python packages and resolves issues encountered in zipped package environments.

@jodal
Copy link
Owner

jodal commented Oct 3, 2024

This is a very reasonable use case. I'll incorporate something like this into the next release.

@jodal
Copy link
Owner

jodal commented Oct 3, 2024

I've released v3.5.0 which uses importlib.resources to load the data files. Hope this solves your issue!

I'd very much appreciate if you added yourself to the list of Biip users at https://github.com/jodal/biip/wiki/Users!

@novikovaTanya
Copy link
Contributor Author

Amazing, thank you so much! 🙇🏻‍♀️

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

Successfully merging this pull request may close these issues.

2 participants