Skip to main content

rezzie: A small library for accessing resources and data shipped with your package without using the pkg_resources API.

Unit test

Background

Shipping data files with your Bioinformatic Python packages is a typical pattern. There are several ways of accessing these data files, but they can be cumbersome and error-prone. One common approach often recommended on StackOverflow is to use the module's __file__ attribute to find the path of the running script and, from there, find the relative path to the resource. But, this approach is fragile and can easily break if the resource is moved, and it doesn't deal with the case where the resource is in a zip file. Another approach is to use the pkg_resources API. But, that module is substantial and has a lot of dependencies, which can cause significant startup times for your command-line tools.

A third-party package called importlib_resources provides a nice API to deal with this problem, and in more recent versions of (Python ≥3.9), it has become part of the standard library and is built on the importlib.resource API of the standard library.

Here, we provide a simple library that has a single function to access the path to any resource shipped with your package. The function returns a pathlib.Path object. You can then read the contents of the file, pass it to a subprocess routine, or do anything else you may need or want to do. We have made it so that it transparently supports versions of Python ≥3.7. When using versions of Python ≥3.9, it uses the standard library importlib_resources API. Otherwise, it uses the importlib_resources package. It will only install the importlib_resources package if the Python version requires it. So, it should keep things lean if you use a recent Python version.

For that reason and many more, we recommend using Python ≥3.10. There are significant performance improvements in the standard library importlib API and notable performances in Python overall to warrant the upgrade.

Installation

You can install rezzie from PyPI using pip:

    pip install rezzie

Usage

To use rezzie, you need to have a package that has resources that you want to access. For example, let's say you have a package called my_package that has a resource called my_resource.txt in a folder called data, that you want to access. You can do that using the rezzie API as follows:

    from rezzie import get_path

    resource_path = get_path("my_package", "data", "my_resource.txt")

The get_path function takes two arguments: the name of the package, and the path and name of the resource.

Example

Let's say you have a package called my_package that has a resource called my_resource.txt in a folder called data, that you want to access.

The my_package package has the following structure:

    my_package
    ├── __init__.py
    ├── data
    │   └── my_resource.txt
    └── __main__.py

Your pyproject.toml file should look like this to ensure the data files are packaged correctly (this assumes using setuptools as the build backend):

    [build-system]
    requires = ["setuptools>=42", "wheel"]
    build-backend = "setuptools.build_meta"

    [project]
    name = "my_package"
    
    [tool.setuptools]
    include-package-data = true

    [tool.setuptools.package-data]
    my_package = ["*.txt"]

The my_package/__init__.py file should look like this:

    from rezzie import get_path

    resource_path = get_path("my_package", "data", "my_resource.txt")

Author

Anders Goncalves da Silva (@andersgs)

Metadata

Release files for rezzie 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rezzie 1.0.0
File Size Uploaded
rezzie-1.0.0.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rezzie 1.0.0
File Interpreter ABI Platform
rezzie-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.5 kB

Release files / rezzie-1.0.0.tar.gz

Download URL rezzie-1.0.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3512becd8f385cb03a66b1f77a2108a11b6bec061ddada891e6e371c245c2537
BLAKE2b-256 checksum
How to use checksums
51b0475d544895a51b324af1ea9255190b67d56411ef4ed39c2f22aa7aa5006f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release files / rezzie-1.0.0-py3-none-any.whl

Download URL rezzie-1.0.0-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8aaeb946903c5211eaed26cd5bd5baa6acd69fd99165c9aab6432210da6604f4
BLAKE2b-256 checksum
How to use checksums
14529342b233ddaf40ca71a66706dd8d35ffb373bb25f37f9cc9e52faebe83f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page