read_text() of a zip file package doesn't find a resource (#48) · Issues · python-devs / importlib_resources · GitLab

This project is archived. Its data is read-only.
read_text() of a zip file package doesn't find a resource
Hi, In issue #44 I reported a problem with `importlib_resources.contents()` and `importlib_resources.read()`, but then the the bug title was changed, and only the first of those was fixed. Sorry if I wasn't clear enough that it wasn't just `contents()`. The remaining problem is, `importlib_resources.read_text()` (and probably similar functions) fails to find resources in zip archives. Here is reproducer -- run on a *nix, in an empty directory, with importlib_resources installed: ```bash #! /bin/bash -ex # Preparation rm -rf demo.pyz demo mkdir -p demo/demo # Populate files to be packaged echo " import importlib_resources def main(): for resource in importlib_resources.contents(__name__): if resource.endswith('.txt'): print('Reading:', resource) text = importlib_resources.read_text(__name__, resource) print('Length:', len(text)) " > demo/demo/__init__.py echo "This is a resource" > demo/demo/resource.txt # Zip everything up python -m zipapp demo -o demo.pyz -m demo:main # Try to run it python --version python demo.pyz ``` Result: ```pycon Reading: resource.txt Traceback (most recent call last): File "/tmp/fghj/__venv__/lib64/python3.6/site-packages/importlib_resources/_py3.py", line 111, in open_text full_path, mode='r', encoding=encoding, errors=errors) NotADirectoryError: [Errno 20] Not a directory: '/tmp/demo.pyz/demo/resource.txt' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/lib64/python3.6/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals) File "demo.pyz/__main__.py", line 3, in <module> File "demo.pyz/demo/__init__.py", line 8, in main File "__venv__/lib64/python3.6/site-packages/importlib_resources/_py3.py", line 149, in read_text with open_text(package, resource, encoding, errors) as fp: File "__venv__/lib64/python3.6/site-packages/importlib_resources/_py3.py", line 125, in open_text raise FileNotFoundError(message) FileNotFoundError: 'resource.txt' resource not found in 'demo' ``` Expected result (what I get when I use `pkg_resources` by “anti-following” the Migration Guide): ``` Reading: resource.txt Length: 19 ```