Skip to content

Commit 150b1e1

Browse files
gaborbernatclaude
andcommitted
🐛 fix(pip): set PIP_USER=0 to prevent --user installs in virtualenvs
When users have pip configured globally to install packages with --user (via pip config --user), pip attempts to use this inside tox virtualenvs where user site-packages aren't visible, causing installation failures. This was fixed in tox 3 but the patch was lost during the tox 4 rewrite. Fixes #3010 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5191a92 commit 150b1e1

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

docs/changelog/3010.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Set ``PIP_USER=0`` environment variable when running pip commands in virtualenvs to prevent pip from attempting
2+
``--user`` installs when users have ``pip config --user`` configured globally — by :user:`gaborbernat`.

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ dependencies = [
5757
"platformdirs>=4.9.1",
5858
"pluggy>=1.6",
5959
"pyproject-api>=1.10",
60-
"tomli>=2.4; python_version<'3.11'",
61-
"typing-extensions>=4.15; python_version<'3.11'",
60+
"tomli>=2.4; python_version < '3.11'",
61+
"typing-extensions>=4.15; python_version < '3.11'",
6262
"virtualenv>=20.36.1",
6363
]
6464
optional-dependencies.completion = [
@@ -96,8 +96,8 @@ test = [
9696
"pytest-timeout>=2.4",
9797
"pytest-xdist>=3.8",
9898
"re-assert>=1.1",
99-
"setuptools>=80.10.2",
100-
"time-machine>=3.2; implementation_name!='pypy'",
99+
"setuptools>=82",
100+
"time-machine>=3.2; implementation_name != 'pypy'",
101101
"wheel>=0.46.3",
102102
]
103103
type = [

src/tox/tox_env/python/virtual_env/api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def runs_on_platform(self) -> str:
176176
def environment_variables(self) -> dict[str, str]:
177177
environment_variables = super().environment_variables
178178
environment_variables["VIRTUAL_ENV"] = str(self.conf["env_dir"])
179+
environment_variables["PIP_USER"] = "0"
179180
return environment_variables
180181

181182
@classmethod

tests/tox_env/python/virtual_env/test_virtualenv_api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,14 @@ def test_list_dependencies_command(tox_project: ToxProjectCreator) -> None:
172172
result.assert_success()
173173
request: ExecuteRequest = execute_calls.call_args[0][3]
174174
assert request.cmd == ["python", "-m", "pip", "freeze"]
175+
176+
177+
def test_pip_user_disabled(tox_project: ToxProjectCreator) -> None:
178+
ini = (
179+
'[testenv]\npackage=skip\ncommands=python -c "import os; print(os.environ.get(\\"PIP_USER\\", \\"NOT_SET\\"))"'
180+
)
181+
proj = tox_project({"tox.ini": ini})
182+
result = proj.run("r", "-e", "py")
183+
result.assert_success()
184+
assert "0" in result.out
185+
assert "NOT_SET" not in result.out

0 commit comments

Comments
 (0)