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.
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.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With