Search code examples
pythonpippackagedependenciespyproject.toml

Usage of direct references in pyproject.toml with hatchling backend


If I understand the documentation for hatchling correctly, in a pyproject.toml with hatchling as a backend, I should be able to to add a local package within the package folder by using the local direct reference schema <NAME> @ {root:uri}/pkg_inside_project.

Here is a minimal non-working example where in the stackoverflow_demo package, I added the dependency to a package called my_local_package via my_local_package @ {root:uri}/my_local_package.

When I clone the repo, go in the folder and try to install stackoverflow_demo by running pip install -e ., I receive a long error (see below).

How can I correctly use the local package direct reference?

Side notes:

  • I am using the -e flag for pip, as I want to be able to install it in editable mode. Both modes, editable and normal, should be supported. At the moment, it doesn't work in either mode and the error is (nearly) the same.
  • I am using pip version 22.2.2
  • I can only use pip to install, no other package manager unfortunately.
  • I cannot install the local package from some remote git repository or so.

The output of pip install with the error (personal information removed):

Looking in indexes: [artifactstore-1], [artifactstore-2]
Obtaining file:///C:/Path/to/demo/stackoverflow_demo
  Installing build dependencies ... done
  Checking if build backend supports build_editable ... done
  Getting requirements to build editable ... done
  Preparing editable metadata (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Preparing editable metadata (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [27 lines of output]
      Traceback (most recent call last):
        File "C:\Path\to\demo\stackoverflow_demo\venv\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 177, in prepare_metadata_for_build_editable
          hook = backend.prepare_metadata_for_build_editable
      AttributeError: module 'hatchling.build' has no attribute 'prepare_metadata_for_build_editable'
     
      During handling of the above exception, another exception occurred:
     
      Traceback (most recent call last):
        File "C:\Path\to\demo\stackoverflow_demo\venv\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 363, in <module>
          main()
        File "C:\Path\to\demo\stackoverflow_demo\venv\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 345, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "C:\Path\to\demo\stackoverflow_demo\venv\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 186, in prepare_metadata_for_build_editable
          whl_basename = build_hook(metadata_directory, config_settings)
        File "C:\Other\Path\to\Local\Temp\1\pip-build-env-4qkba2h_\overlay\Lib\site-packages\hatchling\build.py", line 61, in build_editable
          return os.path.basename(next(builder.build(wheel_directory, ['editable'])))
        File "C:\Other\Path\to\Local\Temp\1\pip-build-env-4qkba2h_\overlay\Lib\site-packages\hatchling\builders\plugin\interface.py", line 80, in build
          self.metadata.validate_fields()
        File "C:\Other\Path\to\Local\Temp\1\pip-build-env-4qkba2h_\overlay\Lib\site-packages\hatchling\metadata\core.py", line 168, in validate_fields
          self.core.validate_fields()
        File "C:\Other\Path\to\Local\Temp\1\pip-build-env-4qkba2h_\overlay\Lib\site-packages\hatchling\metadata\core.py", line 1129, in validate_fields
          getattr(self, attribute)
        File "C:\Other\Path\to\Local\Temp\1\pip-build-env-4qkba2h_\overlay\Lib\site-packages\hatchling\metadata\core.py", line 1017, in dependencies
          self._dependencies = list(self.dependencies_complex)
        File "C:\Other\Path\to\Local\Temp\1\pip-build-env-4qkba2h_\overlay\Lib\site-packages\hatchling\metadata\core.py", line 1001, in dependencies_complex
          raise ValueError(
      ValueError: Dependency #1 of field `project.dependencies` cannot be a direct reference
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

Solution

  • From the issue tracker I received the answer, that I just have to add the following two lines to the pyproject.toml. See the documentation.

    [tool.hatch.metadata]
    allow-direct-references = true