Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide six.pkgname to replace __package__ #301

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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