Barry Warsaw - Get your resources faster, with importlib.resources - PyCon 2018
Why Stack Overflow recommends it
Stack Overflow users link it when explaining why `importlib.resources` is preferred over `__file__` and `pkg_resources` for reading package data, including zipped packages and import performance.
Recommended by 2 people across 2 different months, 2019 to 2024.
What it is
A PyCon 2018 talk by Barry Warsaw on accessing files that live inside Python packages (test data, templates, certificates) without relying on `__file__` or `pkg_resources`. It walks through the motivations for `importlib.resources`, how to use it, its interfaces, and the loader hooks third parties can implement.
What people said
Recommends Enrique G · 2019-10 · Stack Overflow · on “Is this how importlib.resources is supposed to be used?”
On a side note, I would suggest watching Barry Warsaw PyCon 2018 talk 'Get your resources faster, with importlib.resources' [this video] "This talk will step through the motivations behind `importlib.resources`, the library's usage, its interfaces, and the hooks made available to third party packages."
Recommends adam.hendry · 2020-08 · Stack Overflow · on “Get Path to Subpackage in Python”
No, `__file__` is not to be used by `importlib`. This is considered bad practice. Please see Barry Warsaw's PyCon 2018 video "Get your resources faster, with importlib.resources" .
Cites Antti Haapala · 2019-11 · Stack Overflow · on “Access data in package subdirectory”
There is often not point in making an answer that details code that does not work as is, but I believe this to be an exception. Python 3.7 added importlib.resources [LINK] that is supposed to replace pkg_resources . It would work for accessing files within packages that do not have slashes in their names, i.e. foo/ __init__.py module1.py module2.py data/ data.txt data2.txt i.e. you could access data2.txt inside package foo with for example importlib.resources.open_binary('foo', 'data2.txt') but it would fail with an exception for >>> importlib.resources.open_binary('foo', 'data/data.txt') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.7/importli
Cites adam.hendry · 2020-08 · Stack Overflow · on “Get Path to Subpackage in Python”
I have a project that needs updating from python2 to python3. It currently uses pkg_resources , which has significant overhead ( PyCon 2018 - Get Your Resources Faster with importlib.resources, by Barry Warsaw [this video] ). It will be packaged into a zip file. The relevant portions of the package structure are: +-- project/ |__+ __init__.py |__+ main.py |__resources/ | |__+ images/ | | |__ # program images | |__+ logging/ | |__+ custom_logger.py |__log/ |__+ project_log.log Intent for Original Question The intent for the original question was to ask "how does one get the absolute path to a subpackage from within another subpackage", but I did a poor job wording this. I thought since current wor
Cites adam.hendry · 2022-06 · Stack Overflow · on “ImportError with PySide6/shiboken6”
What did i do wrong? I don't believe you did anything wrong. I think this was a known bug in shiboken6 versions <=6.0.0 . It was reported partly in the PySide bug tracker: PYSIDE-932 [LINK] . The issue appeared to be that they were attempting to import from a zip file. From the comments in version 6.3.0 of shiboken6.__init__ : # PYSIDE-932: Python 2 cannot import 'zipfile' for embedding while being imported, itself. # We simply pre-load all imports for the signature extension. # Also, PyInstaller seems not always to be reliable in finding modules. # We explicitly import everything that is needed: The comment says Python 2 cannot import , but it should read PySide2 cannot import because the i
Cites ukBaz · 2023-02 · Stack Overflow · on “Add a data directory outside Python package directory”
Most pythonic? Then probably move the directory inside the package. [this video] You'll have to rename one of your`data` directories Or swap around which one is in the package
Cites BdB · 2024-04 · Stack Overflow · on “Access files within python package with python 3.12 and importlib”
I'm trying to figure out what the best practice is when it comes to reading data from within a package. I understood that in python 3.12 I should use importlib.resources, see e.g. How to read a (static) file from inside a Python package? [LINK] Initially, I organized the package as follows: foo ├── setup.cfg ├── data │ ├── __init__.py │ └── data.csv │ └── data2 │ ├── __init__.py │ └── data2.csv └── src └── foo ├── __init__.py └── bar.py Such that data could be read in bar.py, as follows: import importlib.resources def get_data_file(file_name): return importlib.resources.files("foo.data").joinpath(file_name).read_text() I added the following to setup.cfg: include_package_data = True [options.