uv: uv run fails if project table is missing
I often interact with repositories with python code that has a pyproject.toml for tool configuration only. Unlike https://github.com/astral-sh/uv/issues/6419, I can’t really tell if this is compliant or not, but it became common when black was released. It was one of the first really popular tools that required configuration to be in pyproject.toml.
Steps to reproduce.
$ uv --version
uv 0.4.0
$ mkdir /tmp/test-pyproject
$ cd /tmp/test-pyproject
$ touch pyproject.toml
$ uv run python
error: No `project` table found in: `/tmp/test-pyproject/pyproject.toml`
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 10
- Comments: 18 (5 by maintainers)
I think this should be reopened
it’s frustrating at the moment that trying to use uv with certain projects fails, e.g. if pyproject.yaml exists but only contains settings for
black, which is pretty common, uv simply won’t work at all unless you migrate everything over to pyproject.yamlimo if [project] is missing from pyproject.yaml uv should behave as if pyproject.yaml doesn’t exist at all.
I believe this is being addressed in https://github.com/astral-sh/uv/pull/14113
The main intention of
pyproject.toml(created long before uv was conceived) was to define a python project. It declares its name, version, author, dependencies, etc and also declare how it is built. Basically replacesetup.py,setup.cfgetc. as well as be a common configuration file format for python project tools. However, just likesetup.cfg, it can also store configuration for different tools that you might use in your project, like isort, black, ruff, pytest, mypy, as well as uv. @DeinAlptraum The page you are linking to only refers to uv usingpyproject.tomlas ONE way to configure it. I agree it is very confusing though and it should probably be rewritten to highlight that better. There is more documentation about the project specific parts ofpyproject.tomlstarting at https://docs.astral.sh/uv/concepts/projects/layout/Tools that store its config in
pyproject.tomlwill only read that file for its own configuration and then ignore everything else. It might check that the file is valid toml, but that’s it.Most tools that read
pyproject.tomlcan also read other configuration files likesetup.cfg,tox.ini, their own format.Except black. It only reads
pyproject.toml. Black was adopted by many projects. Even projects that had not yet moved to usingpyproject.tomlfor the project metadata. This means that there are a lot ofpyproject.tomlfiles out there without a[project]table. (We are likely outliers, but I found 1291pyproject.tomlfiles at the company where I work, of which only 374 have a[project]table).uv is not like most tools. It has many different use cases. Since 0.4 you can run it as a project tool. You can also use it as a pip and/or pip-compile replacement.
However, what makes things everything extra confusing is that you CAN ALSO use project specific commands like
runoutside of a project, where there is nopyproject.toml. What seems to happen here is that a special implicit project is created on the fly.Because that works, it is very confusing when it doesn’t work if you are in a directory of a python project that has a
pyproject.tomlwithout a[project]table.Personally I am ok if requires a project table in
pyproject.tomlfiles. I am teaching all my colleagues how to migrate topyproject.tomlfiles anyway. But I would also be totally ok if uv would treat a missing project table the same as a missingpyproject.tomlfile. I think this will make the transition to uv smoother.There would of course have to be some corner cases that I can’t figure out now. What if you find two
pyproject.tomlfiles. One in the root of a project with a[project]and another in a subdirectory without a[project]. Both files have[tool.uv...]sections. Will one[tool.uv...]section still override the other? Or can the validation rule be changed to only require a[project]table if there is also a[tool.uv...]section?I’m not familiar with what a pyproject.toml is supposed to do and not to do, and I don’t really care tbh. If I can’t even query my python version through uv anymore just because this file is present, I think that’s problematic. I’m also not sure why this file affects the
uv run python [...]command in the first place: afaic this should act the same as if I didsource <path to venv>/bin/activate && python [...], which doesn’t care about the pyproject.toml at all. Perhaps throwing a warning about an ill-formed pyproject.toml and then continuing normally would be better?Whether there’s a use-case for this, I don’t know. I can only tell you that people seem to be using it that way, because I’ve already run into that error on another 2 repos since I posted that comment a couple hours ago.
I just looked at https://docs.astral.sh/uv/configuration/files/, which says: “If a
pyproject.tomlfile is found, uv will read configuration from the[tool.uv]table. […] (If there is no such table, thepyproject.tomlfile will be ignored, and uv will continue searching in the directory hierarchy.)” which is not the case at the moment.As far as I can tell the issue also creates a Catch-22 that prevents you from using uv to run tools (ini2toml, pdm) to migrate from setup.cfg to pyproject.toml.
I just ran into this as a friction point on a project which is mostly not Python but has a couple of utility scripts. It had a
pyproject.tomlfile which existed only to hold Ruff’s settings (migrated from Black) so the fix ended up being renaming the file to.ruff.tomlbut it was an annoying bit of friction sinceuv run --scriptis otherwise so painless for single-file programs.