[Import-SIG] Loading Resources From a Python Module/Package

Barry Warsaw barry at python.org
Sat Jan 31 19:11:42 CET 2015


On Jan 31, 2015, at 11:43 AM, Donald Stufft wrote:

>I think we do want to allow directories, it’s not unusual to have something
>like:
>
>warehouse
>├── __init__.py
>├── templates
>│   ├── accounts
>│   │   └── profile.html
>│   └── hello.html
>├── utils
>│   └── mapper.py
>└── wsgi.py
>
>Conceptually templates isn’t a package (even though with namespace packages
>it kinda is) and I’d want to load profile.html by doing something like:
>
>importlib.resources.get_bytes(“warehouse”, “templates/accounts/profile.html”)

I understand there's a conceptual wart, but I have no problem dropping an
empty __init__.py file in those subdirectories and then using:

importlib.resources.get_bytes('warehouse.templates.accounts', 'profile.html')

And given how much easier it makes life from an implementation and description
standpoint, I think it's a fine compromise.

Cheers,
-Barry


More information about the Import-SIG mailing list