Editable install with data folder does not work with pyproject.toml and hatch

Viewed 43

I have a Python package that uses data files stored in the src/data directory. I can successfully install the package using pip install . but using pip install -e . does not work because the data folder is not found. How can I perform an editable install of this package?

mypackage
├── README.md
├── examples
│   └── example.py
├── pyproject.toml
├── src
│   ├── data
│   │   └── fruits.csv
│   └── mypackage
│       ├── __init__.py
│       ├── adder.py
│       └── fruit_reader.py
└── tests
    ├── test_adder.py
    └── test_read_fruits.py

The contents of the pyproject.toml file is shown below. Notice the src/data folder is included as mypackage/data during the build process.

# pyproject.toml

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "mypackage"
version = "0.1"
authors = [
  { name="Example Author", email="author@example.com" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.9"
classifiers = [
  "Programming Language :: Python :: 3",
  "License :: OSI Approved :: MIT License",
  "Operating System :: OS Independent",
]

[tool.hatch.build.force-include]
"src/data" = "mypackage/data"

The read_fruits() function reads the CSV file located in the data folder as shown here.

# fruit_reader.py

import pandas as pd
from pathlib import Path

def read_fruits():
    """
    Read fruit data from CSV file.
    """
    path = Path(__file__).parent / 'data/fruits.csv'
    print(f'File path is\n{path}')

    df = pd.read_csv(path)
    print(f'Pandas dataframe is\n{df}')
0 Answers
Related