contents() of a zip file package returns incorrect results
Packages (directories) inside .pyz
archives seem to confuse importlib_resources 0.1.
importlib_resources.contents()
can return content for unrelated directories -- even for directories that aren't packages.
importlib_resources.read()
fails to find resources that .contents()
found -- even those that are in the archive.
Here is reproducer -- run on a *nix, in an empty directory, with importlib_resources installed:
#! /bin/bash -ex
rm -rf demo.pyz demo unrelated
mkdir demo
echo "
import importlib_resources
def main():
for resource in sorted(importlib_resources.contents(__name__)):
print('Resource:', resource)
for resource in sorted(importlib_resources.contents(__name__)):
print('Reading:', resource)
importlib_resources.read(__name__, resource)
" > demo/__init__.py
echo > demo/resource.txt
mkdir unrelated
echo > unrelated/unrelated_resource.txt
python -m zipapp -o demo.pyz . -m demo:main
python demo.pyz
Result:
Resource: __init__.py
Resource: resource.txt
Resource: unrelated_resource.txt
Reading: __init__.py
Traceback (most recent call last):
...
FileNotFoundError: '__init__.py' resource not found in 'demo'
Expected (and what I get when I use pkg_resources
by “anti-following” the Migration Guide):
Resource: __init__.py
Resource: resource.txt
Reading: __init__.py
357
Reading: resource.txt
1
Edited by Barry Warsaw