Search code examples
pythonbuildhatch

Using Hatch, is there a way I can have a local dependency whose location is not hard-coded?


I have two non-public Hatch projects A and B. B depends on A. I want developers to be able to clone A and B to locations of their choice and have B depend on A without having to rewrite pyproject.toml. Can I do this? I’d be okay with a prompt asking for a location, or an auxiliary file that can be ignored from VCS, or require the projects to be in a specific folder (X/A and X/B, e.g.). I have looked at the dependency specifier documentation, which seems to at best allow file:// URIs, which, to my knowledge, are always absolute.


Solution

  • It is possible to look up a project using a relative path using Hatch’s context formatting. You can write the following in project-b/pyproject.toml:

    [project]
    dependencies = [
        "project-a @ file://{root}/../project-b"
    ]
    

    Note that we omit the leading slash from the path and the URI has only two slashes following file:, because {root} is replaced with the absolute path to the project.

    If the folder structure is as such:

    /
    ├── project-a
    │   ├── pyproject.toml
    │   └── …
    └── project-b
        ├── pyproject.toml
        └── …
    

    then {root} inside /project-a/pyproject.toml will resolve to /project-a (or possibly /project-a/, I haven’t checked, but it doesn’t matter, with an extra slash you can be sure it’ll work).