Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having a .python-version or .python-versions file breaks Python version definition in Nox with uv backend #931

Open
MicaelJarniac opened this issue Feb 11, 2025 · 4 comments · May be fixed by #933
Labels

Comments

@MicaelJarniac
Copy link
Contributor

Current Behavior

If there is a .python-version or .python-versions file in the project's root, uv will default to that after a uv sync, and ignore the Python version specified in the Noxfile.

Expected Behavior

It should ignore these files and use the Python version defined by Nox.

Steps To Reproduce

  1. uv init nox-uv
  2. code nox-uv
  3. uv add --dev "nox[uv]"
  4. uv sync
  5. Create the following noxfile.py in the root:
import nox

nox.options.default_venv_backend = "uv"


@nox.session(python=["3.8", "3.9"])
def py_ver(session: nox.Session) -> None:
    session.run_install(
        "uv",
        "sync",
        "--no-group=dev",
        env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
    )
    session.run("python", "--version")
  1. nox
  2. Notice how it'll run all sessions with Python 3.13.
❯ nox
nox > Running session py_ver-3.8
nox > Creating virtual environment (uv) using python3.8 in .nox/py_ver-3-8
nox > uv sync --no-group=dev
Using CPython 3.13.2
Removed virtual environment at: .nox/py_ver-3-8
Creating virtual environment at: .nox/py_ver-3-8
Resolved 13 packages in 0.92ms
Audited in 0.01ms
nox > python --version
Python 3.13.2
nox > Session py_ver-3.8 was successful.
nox > Running session py_ver-3.9
nox > Creating virtual environment (uv) using python3.9 in .nox/py_ver-3-9
nox > uv sync --no-group=dev
Using CPython 3.13.2
Removed virtual environment at: .nox/py_ver-3-9
Creating virtual environment at: .nox/py_ver-3-9
Resolved 13 packages in 1ms
Audited in 0.01ms
nox > python --version
Python 3.13.2
nox > Session py_ver-3.9 was successful.
nox > Ran multiple sessions:
nox > * py_ver-3.8: success
nox > * py_ver-3.9: success

Environment

- OS: Ubuntu 24.04
- Python: 3.13
- Nox: 2025.2.9

Anything else?

I'm following the official Nox recipe for uv found here:
https://nox.thea.codes/en/stable/cookbook.html#using-a-lockfile

I believe this may be solved by adapting the recipe.

@henryiii
Copy link
Collaborator

You can add --no-config, though that will also ignore uv.toml. Thanks to @zanieb for the suggestion! We might want to add that to the recipe for now until there's a more fine-grained way to avoid replacing the venv.

@MicaelJarniac
Copy link
Contributor Author

--no-config didn't seem to work.

❯ nox
nox > Running session py_ver-3.8
nox > Creating virtual environment (uv) using python3.8 in .nox/py_ver-3-8
nox > uv sync --no-group=dev --no-config
Using CPython 3.13.2
Removed virtual environment at: .nox/py_ver-3-8
Creating virtual environment at: .nox/py_ver-3-8
Resolved 13 packages in 1ms
Audited in 0.02ms
nox > python --version
Python 3.13.2
nox > Session py_ver-3.8 was successful.
nox > Running session py_ver-3.9
nox > Creating virtual environment (uv) using python3.9 in .nox/py_ver-3-9
nox > uv sync --no-group=dev --no-config
Using CPython 3.13.2
Removed virtual environment at: .nox/py_ver-3-9
Creating virtual environment at: .nox/py_ver-3-9
Resolved 13 packages in 1ms
Audited in 0.02ms
nox > python --version
Python 3.13.2
nox > Session py_ver-3.9 was successful.
nox > Ran multiple sessions:
nox > * py_ver-3.8: success
nox > * py_ver-3.9: success

@henryiii
Copy link
Collaborator

henryiii commented Feb 11, 2025

Is your requires-python correct? It will ignore the request if it doesn't match that, and uv sets it to >=3.13 by default. I made this mistake the first time, too.

Another way to do it is to set it via -p, like this:

@nox.session(python=["3.8", "3.9"])
def py_ver(session: nox.Session) -> None:
    session.run_install(
        "uv",
        "sync",
        "--no-default-groups",
        f"-p{session.virtualenv.location}",
        env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
    )
    session.run("python", "--version")

@MicaelJarniac
Copy link
Contributor Author

It was a bad requires-python indeed, thanks!

@henryiii henryiii linked a pull request Feb 12, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants