Skip to content

Use importlib to load files inside the package #167

@alexdewar

Description

@alexdewar

I've noticed this in the places where the various JSON schemas are being loaded, but this may be a problem elsewhere in the code. For example, see here: https://github.com/ImperialCollegeLondon/virtual_rainforest/blob/7f445d9e6fd729c7f398576ba51c30b5efda0e55/virtual_rainforest/models/plants/__init__.py#L11

The code looks something like this:

schema_file = Path(__file__).parent.resolve() / "plants_schema.json"

Intuitively, this should be correct, but unfortunately it doesn't work with all the weird and wonderful ways Python code can be packaged, so technically you should always use the importlib.resources module: https://docs.python.org/3/library/importlib.resources.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions