Skip to content

Conversation

@graingert
Copy link
Contributor

No description provided.

@graingert
Copy link
Contributor Author

graingert commented Oct 3, 2019

to use replace __package__ with six.pkgname(globals())

useful when using the importlib_resources backport

from __future__ import absolute_import

try:
    from importlib import resources as importlib_resources
except ImportError:
    import importlib_resources


resource_text = importlib_resources.read_text(__package__, "file.csv")

on Python2:

TypeError: None is not a package
from __future__ import absolute_import

import six

try:
    from importlib import resources as importlib_resources
except ImportError:
    import importlib_resources


resource_text = importlib_resources.read_text(six.pkgname(globals()), "file.csv")

@graingert
Copy link
Contributor Author

@benjaminp ^

@benjaminp
Copy link
Owner

I'm a bit confused why import_resources.read_ exist at all when pkgutil.get_data has existed for many years and works if you just pass __name__.

@graingert
Copy link
Contributor Author

@benjaminp here's the issue where it landed: https://bugs.python.org/issue32248

@graingert
Copy link
Contributor Author

pkgutil.get_data doesn't work in various cases, such as namespace packages

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