nox icon indicating copy to clipboard operation
nox copied to clipboard

Python version from `.python-version` is not overridden by the one of the session

Open paduszyk opened this issue 1 month ago • 0 comments

Current Behavior

In my project, I have a .python-version file to pin a version for development and CI:

# .python-version
3.14

and noxfile.py:

from __future__ import annotations

import nox

nox.options.default_venv_backend = "uv"


@nox.session(requires=["uv"])
@nox.parametrize(
    "python",
    [
        "3.10",
        "3.11",
        "3.12",
        "3.13",
        "3.14",
    ],
)
def pytest(session: nox.Session) -> None:
    session.run_install(
        "uv",
        "sync",
        "--no-dev",
        "--group=pytest",
        "--frozen",
        env={
            "UV_PROJECT_ENVIRONMENT": session.virtualenv.location,
        },
    )
    session.run("pytest")

I noticed that Nox creates the environments based on Python 3.14 only. If I remove the .python-version file, everything works as expected, i.e. the respective Python versions are installed and tested.

Expected Behavior

Nox should not care about the .python-version file and enforce the Python version specified by the session's parameter.

Steps To Reproduce

  1. Run nox with .python-version (with 3.14 for a reference).
  2. Check .nox/ contents — Python 3.14 is found in each environment.
  3. rm -rf .nox/.
  4. rm .python-version.
  5. Run nox again.
  6. Check .nox/ contents — now the Python versions are as expected.

Environment

- OS:
- Python:
- Nox:

Anything else?

A workaround:

@nox.session(requires=["uv"])
@nox.parametrize(
    "python",
    [
        "3.10",
        "3.11",
        "3.12",
        "3.13",
        "3.14",
    ],
)
def pytest(session: nox.Session, python: str) -> None:  # 'python' as explicit parameter
    session.run_install(
        "uv",
        "sync",
        f"--python={python}",  # the proper version installed via uv
        "--no-dev",
        "--group=pytest",
        "--frozen",
        env={
            "UV_PROJECT_ENVIRONMENT": session.virtualenv.location,
        },
    )
    session.run("pytest")

paduszyk avatar Nov 23 '25 20:11 paduszyk