Skip to content

fix: PYTHONPATH only partially removed#1038

Closed
henryiii wants to merge 3 commits into
pypa:mainfrom
henryiii:henryiii/fix/PYTHONPATH
Closed

fix: PYTHONPATH only partially removed#1038
henryiii wants to merge 3 commits into
pypa:mainfrom
henryiii:henryiii/fix/PYTHONPATH

Conversation

@henryiii

@henryiii henryiii commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Description

Remove the PYTHONPATH setting from _pip_env. From the "possible solution", I think this should help #1037?

🤖 Assisted-by: OpenCode:Kimi-K2.5 - used to quickly implement the suggestion and a few tests, then iterated by hand on this to just remove vs. override.

Changelog

  • Added changelog fragment: docs/changelog/<pr_number>.<type>.rst
    • Types: feature, bugfix, doc, removal, misc
    • Example: 123.feature.rst containing Add custom backend support - by :user:`yourname`

Checklist

  • Tests pass locally (tox)
  • Code follows project style (tox -e fix)
  • Type checks pass (tox -e type)
  • Documentation builds (tox -e docs)

@henryiii henryiii force-pushed the henryiii/fix/PYTHONPATH branch from 4a62725 to 8cf1bb1 Compare April 14, 2026 22:11
@henryiii henryiii changed the title fix: PYTHOPATH only partially removed fix: PYTHONPATH only partially removed Apr 14, 2026
@henryiii henryiii force-pushed the henryiii/fix/PYTHONPATH branch from 8cf1bb1 to d4c1ae2 Compare April 14, 2026 23:34
@henryiii henryiii marked this pull request as ready for review April 14, 2026 23:36
@henryiii henryiii requested review from FFY00 and layday as code owners April 14, 2026 23:36
@henryiii

Copy link
Copy Markdown
Contributor Author

Once @layday reviews, this is good to merge, and we can have a quick patch release.

@layday

layday commented Apr 15, 2026

Copy link
Copy Markdown
Member

What happens if the outer pip is on the Python path? _has_dependency respects PYTHONPATH.

This will always be the case e.g. in a Nix dev shell.

@henryiii

Copy link
Copy Markdown
Contributor Author

As long as its path gets resolved correctly, shouldn't it be fine? You don't been PYTHONPATH set to call pip.

@layday

layday commented Apr 15, 2026

Copy link
Copy Markdown
Member

You do if pip itself is on PYTHONPATH. This is with Nix:

$ echo $PYTHONPATH
/nix/store/dfkim8vahgdqhk1lrxz1j4mcighlrk99-python3-3.14.3/lib/python3.14/site-packages:/nix/store/yic25g8alpkdjyfkbim9741cpnj8yin8-python3.14-pip-25.3/lib/python3.14/site-packages
$ python -m pip -V
pip 25.3 from /nix/store/yic25g8alpkdjyfkbim9741cpnj8yin8-python3.14-pip-25.3/lib/python3.14/site-packages/pip (python 3.14)
$ PYTHONPATH= python -m pip -V
/nix/store/dfkim8vahgdqhk1lrxz1j4mcighlrk99-python3-3.14.3/bin/python: No module named pip

_PipBackend._has_valid_outer_pip will be true but when you try to run it it won't be there.

@henryiii

henryiii commented Apr 15, 2026

Copy link
Copy Markdown
Contributor Author

Ahh, we do use python -m pip, you are correct.

Should we just revert the PYTHONPATH changes in the last release?

Or we could disable PYTHONPATH when looking for pip. But it looks like we use importlib.metadata.distributions, so that not be easy.

We could also just refuse to look for pip if PYTHONPATH is set.

@layday

layday commented Apr 15, 2026

Copy link
Copy Markdown
Member

Should we just revert the PYTHONPATH changes in the last release?

Meaning #1024? It looks like it isolates the backend runner, but the linked issue (#405) is about PYTHONPATH leaking into ensurepip, which was fixed in python/cpython#92857.

Or we could disable PYTHONPATH when looking for pip.

How do we do that other than by attempting to parse and subtract PYTHONPATH from sys.path or by spawning a subprocess without PYTHONPATH and performing the dependency check there? Neither option sounds particularly appealing to me.

We could just try passing --ignore-installed to pip, but how does that interact with --python?

@henryiii

henryiii commented Apr 15, 2026

Copy link
Copy Markdown
Contributor Author

I've not used that before, we could try? Hopefully it's not new so we don't get bit with even more version-specific behavior. :) (Edit: looks like it's old, good)

@henryiii henryiii marked this pull request as draft April 16, 2026 05:08
henryiii and others added 3 commits April 16, 2026 01:08
Assisted-by: OpenCode:Kimi-K2.5
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
@henryiii henryiii force-pushed the henryiii/fix/PYTHONPATH branch from 7dbbe76 to bcdb4f0 Compare April 16, 2026 05:09
@henryiii

Copy link
Copy Markdown
Contributor Author

Coverage is a bit short, but I tried something different. I tried to create a couple of tests to mimic this (with AI help), and implemented its suggestion as a fix.

For --ignore-installed, I think it's a net positive (as nothing should be there if we are making an isolated environment), but couldn't that still have issues with leakage, if something overrides or adds to the environment?

@layday

layday commented Apr 16, 2026

Copy link
Copy Markdown
Member

Can you be a bit more specific about --ignore-installed?

@henryiii

Copy link
Copy Markdown
Contributor Author

Oh, I think I was thinking about usage too, yeah, that might work. I'll make an alternate PR with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants