Skip to content

Some nox tests fails when PYTHONPATH is set #1044

@mtelka

Description

@mtelka

If you run nox tests with PYTHONPATH set and you do not have all dependencies installed locally then some tests could fail. In my case I do not have cowsay installed and when I do this:

/usr/bin/env -i \
        PYTHONPATH=/data/builds/ul-workspace/components/python/nox/build/prototype/i386//usr/lib/python3.14/vendor-packages \
        PATH=/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/bin:/usr/bin/amd64:/usr/bin:/usr/gnu/bin:/usr/sbin/amd64:/usr/sbin \
        PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 \
        /usr/bin/python3.14 -m pytest \
        --color=no --cache-clear tests/test_main.py::test_noxfile_script_mode -vv

Then I see this failure:

___________________________ test_noxfile_script_mode ___________________________
  
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7fffac93d9b0>

    def test_noxfile_script_mode(monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.delenv("NOX_SCRIPT_MODE", raising=False)
        job = subprocess.run(
            [
                sys.executable,
                "-m",
                "nox",
                "-f",
                Path(RESOURCES) / "noxfile_script_mode.py",
                "-s",
                "example",
            ],
            check=False,
            capture_output=True,
            text=True,
            encoding="utf-8",
        )
        print(job.stdout)
        print(job.stderr)
>       assert job.returncode == 0
E       assert 1 == 0
E        +  where 1 = CompletedProcess(args=['/usr/bin/python3.14', '-m', 'nox', '-f', PosixPath('/data/builds/ul-workspace/components/python/nox/build/amd64-3.14/tests/resources/noxfile_script_mode.py'), '-s', 'example'], returncode=1, stdout='Requirement already satisfied: nox in /data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages (2025.11.12)\nCollecting cowsay\n  Using cached cowsay-6.1-py3-none-any.whl.metadata (5.6 kB)\nCollecting argcomplete<4,>=1.9.4 (from nox)\n  Using cached argcomplete-3.6.3-py3-none-any.whl.metadata (16 kB)\nCollecting attrs>=24.1 (from nox)\n  Using cached attrs-25.4.0-py3-none-any.whl.metadata (10 kB)\nCollecting colorlog<7,>=2.6.1 (from nox)\n  Using cached colorlog-6.10.1-py3-none-any.whl.metadata (11 kB)\nCollecting dependency-groups>=1.1 (from nox)\n  Using cached dependency_groups-1.3.1-py3-none-any.whl.metadata (2.3 kB)\nCollecting humanize>=4 (from nox)\n  Using cached humanize-4.15.0-py3-none-any.whl.metadata (7.8 kB)\nCollecting packaging>=21 (from nox)\n  Using cached packaging-25.0-py3-none-any.whl.metadata (3.3 kB)\nCollecting virtualenv>=20.15 (from nox)\n  Using cached virtualenv-20.35.4-py3-none-any.whl.metadata (4.6 kB)\nCollecting distlib<1,>=0.3.7 (from virtualenv>=20.15->nox)\n  Using cached distlib-0.4.0-py2.py3-none-any.whl.metadata (5.2 kB)\nCollecting filelock<4,>=3.12.2 (from virtualenv>=20.15->nox)\n  Using cached filelock-3.20.1-py3-none-any.whl.metadata (2.1 kB)\nCollecting platformdirs<5,>=3.9.1 (from virtualenv>=20.15->nox)\n  Using cached platformdirs-4.5.1-py3-none-any.whl.metadata (12 kB)\nUsing cached argcomplete-3.6.3-py3-none-any.whl (43 kB)\nUsing cached colorlog-6.10.1-py3-none-any.whl (11 kB)\nUsing cached cowsay-6.1-py3-none-any.whl (25 kB)\nUsing cached attrs-25.4.0-py3-none-any.whl (67 kB)\nUsing cached dependency_groups-1.3.1-py3-none-any.whl (8.7 kB)\nUsing cached humanize-4.15.0-py3-none-any.whl (132 kB)\nUsing cached packaging-25.0-py3-none-any.whl (66 kB)\nUsing cached virtualenv-20.35.4-py3-none-any.whl (6.0 MB)\nUsing cached distlib-0.4.0-py2.py3-none-any.whl (469 kB)\nUsing cached filelock-3.20.1-py3-none-any.whl (16 kB)\nUsing cached platformdirs-4.5.1-py3-none-any.whl (18 kB)\nInstalling collected packages: distlib, platformdirs, packaging, humanize, filelock, cowsay, colorlog, attrs, argcomplete, virtualenv, dependency-groups\n\nSuccessfully installed argcomplete-3.6.3 attrs-25.4.0 colorlog-6.10.1 cowsay-6.1 dependency-groups-1.3.1 distlib-0.4.0 filelock-3.20.1 humanize-4.15.0 packaging-25.0 platformdirs-4.5.1 virtualenv-20.35.4\n', stderr='nox > Creating virtual environment (virtualenv) using python3.14 in .nox/_nox_script_mode\nTraceback (most recent call last):\n  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386//usr/bin/nox", line 8, in <module>\n    sys.exit(main())\n             ~~~~^^\n  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/_cli.py", line 202, in nox_main\n    _main(main_ep=True)\n    ~~~~~^^^^^^^^^^^^^^\n  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/_cli.py", line 275, in _main\n    exit_code = execute_workflow(args)\n  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/_cli.py", line 55, in execute_workflow\n    return workflow.execute(\n           ~~~~~~~~~~~~~~~~^\n        global_config=args,\n        ^^^^^^^^^^^^^^^^^^^\n    ...<10 lines>...\n        ),\n        ^^\n    )\n    ^\n  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/workflow.py", line 61, in execute\n    return_value = function_(*args, **kwargs)\n  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/tasks.py", line 138, in load_nox_module\n    return _load_and_exec_nox_module(global_config)\n  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/tasks.py", line 83, in _load_and_exec_nox_module\n    loader.exec_module(module)\n    ~~~~~~~~~~~~~~~~~~^^^^^^^^\n  File "<frozen importlib._bootstrap_external>", line 759, in exec_module\n  File "<frozen importlib._bootstrap>", line 491, in _call_with_frames_removed\n  File "/data/builds/ul-workspace/components/python/nox/build/amd64-3.14/tests/resources/noxfile_script_mode.py", line 5, in <module>\n    import cowsay\nModuleNotFoundError: No module named \'cowsay\'\n').returncode

/data/builds/ul-workspace/components/python/nox/build/amd64-3.14/tests/test_main.py:1090: AssertionError
----------------------------- Captured stdout call -----------------------------
Requirement already satisfied: nox in /data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages (2025.11.12)
Collecting cowsay
  Using cached cowsay-6.1-py3-none-any.whl.metadata (5.6 kB)
Collecting argcomplete<4,>=1.9.4 (from nox)
  Using cached argcomplete-3.6.3-py3-none-any.whl.metadata (16 kB)
Collecting attrs>=24.1 (from nox)
  Using cached attrs-25.4.0-py3-none-any.whl.metadata (10 kB)
Collecting colorlog<7,>=2.6.1 (from nox)
  Using cached colorlog-6.10.1-py3-none-any.whl.metadata (11 kB)
Collecting dependency-groups>=1.1 (from nox)
  Using cached dependency_groups-1.3.1-py3-none-any.whl.metadata (2.3 kB)
Collecting humanize>=4 (from nox)
  Using cached humanize-4.15.0-py3-none-any.whl.metadata (7.8 kB)
Collecting packaging>=21 (from nox)
  Using cached packaging-25.0-py3-none-any.whl.metadata (3.3 kB)
Collecting virtualenv>=20.15 (from nox)
  Using cached virtualenv-20.35.4-py3-none-any.whl.metadata (4.6 kB)
Collecting distlib<1,>=0.3.7 (from virtualenv>=20.15->nox)
  Using cached distlib-0.4.0-py2.py3-none-any.whl.metadata (5.2 kB)
Collecting filelock<4,>=3.12.2 (from virtualenv>=20.15->nox)
  Using cached filelock-3.20.1-py3-none-any.whl.metadata (2.1 kB)
Collecting platformdirs<5,>=3.9.1 (from virtualenv>=20.15->nox)
  Using cached platformdirs-4.5.1-py3-none-any.whl.metadata (12 kB)
Using cached argcomplete-3.6.3-py3-none-any.whl (43 kB)
Using cached colorlog-6.10.1-py3-none-any.whl (11 kB)
Using cached cowsay-6.1-py3-none-any.whl (25 kB)
Using cached attrs-25.4.0-py3-none-any.whl (67 kB)
Using cached dependency_groups-1.3.1-py3-none-any.whl (8.7 kB)
Using cached humanize-4.15.0-py3-none-any.whl (132 kB)
Using cached packaging-25.0-py3-none-any.whl (66 kB)
Using cached virtualenv-20.35.4-py3-none-any.whl (6.0 MB)
Using cached distlib-0.4.0-py2.py3-none-any.whl (469 kB)
Using cached filelock-3.20.1-py3-none-any.whl (16 kB)
Using cached platformdirs-4.5.1-py3-none-any.whl (18 kB)
Installing collected packages: distlib, platformdirs, packaging, humanize, filelock, cowsay, colorlog, attrs, argcomplete, virtualenv, dependency-groups

Successfully installed argcomplete-3.6.3 attrs-25.4.0 colorlog-6.10.1 cowsay-6.1 dependency-groups-1.3.1 distlib-0.4.0 filelock-3.20.1 humanize-4.15.0 packaging-25.0 platformdirs-4.5.1 virtualenv-20.35.4

nox > Creating virtual environment (virtualenv) using python3.14 in .nox/_nox_script_mode
Traceback (most recent call last):
  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386//usr/bin/nox", line 8, in <module>
    sys.exit(main())
             ~~~~^^
  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/_cli.py", line 202, in nox_main
    _main(main_ep=True)
    ~~~~~^^^^^^^^^^^^^^
  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/_cli.py", line 275, in _main
    exit_code = execute_workflow(args)
  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/_cli.py", line 55, in execute_workflow
    return workflow.execute(
           ~~~~~~~~~~~~~~~~^
        global_config=args,
        ^^^^^^^^^^^^^^^^^^^
    ...<10 lines>...
        ),
        ^^
    )
    ^
  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/workflow.py", line 61, in execute
    return_value = function_(*args, **kwargs)
  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/tasks.py", line 138, in load_nox_module
    return _load_and_exec_nox_module(global_config)
  File "/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/lib/python3.14/vendor-packages/nox/tasks.py", line 83, in _load_and_exec_nox_module
    loader.exec_module(module)
    ~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 759, in exec_module
  File "<frozen importlib._bootstrap>", line 491, in _call_with_frames_removed
  File "/data/builds/ul-workspace/components/python/nox/build/amd64-3.14/tests/resources/noxfile_script_mode.py", line 5, in <module>
    import cowsay
ModuleNotFoundError: No module named 'cowsay'

When I add .nox/_nox_script_mode/... to PYTHONPATH then the test pass. This works:

/usr/bin/env -i \
        PYTHONPATH=/data/builds/ul-workspace/components/python/nox/build/prototype/i386//usr/lib/python3.14/vendor-packages:.nox/_nox_script_mode/lib/python3.14/site-packages \
        PATH=/data/builds/ul-workspace/components/python/nox/build/prototype/i386/usr/bin:/usr/bin/amd64:/usr/bin:/usr/gnu/bin:/usr/sbin/amd64:/usr/sbin \
        PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 \
        /usr/bin/python3.14 -m pytest \
        --color=no --cache-clear tests/test_main.py::test_noxfile_script_mode -vv

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions