Fix resource file access to support zipped package environments #324
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:Problem
The current implementation uses
pathlib
and__file__
to construct a file path to_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:
pathlib
and__file__
withimportlib.resources
.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
Python Documentation -
importlib.resources
PEP 273 – Import Modules from Zip Archives
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.