nox
nox copied to clipboard
Python version from `.python-version` is not overridden by the one of the session
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
- Run
noxwith.python-version(with3.14for a reference). - Check
.nox/contents — Python 3.14 is found in each environment. -
rm -rf .nox/. -
rm .python-version. - Run
noxagain. - 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")