Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying local relative dependency in pyproject.toml

I have the following project structure:

root
  - sample/
    - src/
    - tests/
    - pyproject.toml
  - libs/
    - lol/
      - src/
      - tests/
      - pyproject.toml

I'd like to specify lol as a dependency for sample in sample/pyproject.toml. How it can be done? I've tried:

dependencies = [ 
   "lol @ file://libs/lol"
]

But it gives me:

ValueError: non-local file URIs are not supported on this platform: 'file://libs/lol'

and that's ok however I cannot put absolute path here since this is going to be shared code. Same for file://./lib/lol.

What can be done about that? Can I use env variables here, or some placeholders? I don't want to use tools like poetry.

like image 252
Opal Avatar asked Aug 31 '25 18:08

Opal


2 Answers

You can turn your file path from relative to absolute by using a $PROJECT_ROOT env var and injecting it into the path itself:

dependencies = [ 
   "lol @ file:///${PROJECT_ROOT}/libs/lol"
]

pdm or uv will inject this environment variable for you (pdm docs, uv PR). Otherwise, you'll need to assign it yourself. That can be done several ways but it depends on your project and is probably out of scope for this question.

like image 98
AndrewKS Avatar answered Sep 02 '25 06:09

AndrewKS


Instead of specifying the dependencies in pyproject.toml, mark them as dynamic:

[project]
# Replace this:
dependencies = [
    ...
]
# With:
dynamic = ["dependencies"]

Then create a minimal setup.py as explained here:

from pathlib import Path
from setuptools import setup

# This is where you add any fancy path resolution to the local lib:
local_path: str = (Path(__file__).parent / "somewhere").as_uri()

setup(
    install_requires=[
        f"package-name @ {local_path}",
        # other dependencies...
    ]
)

Using setup.py alongside pyproject.toml is mentioned in the setuptools docs as a way to configure "the dynamic parts".

like image 21
Dev-iL Avatar answered Sep 02 '25 06:09

Dev-iL