Skip to content

Commit

Permalink
Don't suggest --user in virtual environment
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr authored and xavfernandez committed Apr 9, 2021
1 parent 0f537c8 commit d878727
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions news/9409.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``--user`` is no longer suggested incorrectly when pip fails with a permission
error in a virtual environment.
7 changes: 5 additions & 2 deletions src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
write_output,
)
from pip._internal.utils.temp_dir import TempDirectory
from pip._internal.utils.virtualenv import virtualenv_no_global
from pip._internal.utils.virtualenv import (
running_under_virtualenv,
virtualenv_no_global,
)
from pip._internal.wheel_builder import (
BinaryAllowedPredicate,
build,
Expand Down Expand Up @@ -725,7 +728,7 @@ def create_os_error_message(error, show_traceback, using_user_site):
user_option_part = "Consider using the `--user` option"
permissions_part = "Check the permissions"

if not using_user_site:
if not running_under_virtualenv() and not using_user_site:
parts.extend([
user_option_part, " or ",
permissions_part.lower(),
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_command_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from pip._vendor.packaging.requirements import Requirement

from pip._internal.commands import install
from pip._internal.commands.install import (
create_os_error_message,
decide_user_install,
Expand Down Expand Up @@ -109,7 +110,8 @@ def test_rejection_for_location_requirement_options():
' permissions.\n'),
])
def test_create_os_error_message(
error, show_traceback, using_user_site, expected
monkeypatch, error, show_traceback, using_user_site, expected
):
monkeypatch.setattr(install, "running_under_virtualenv", lambda: False)
msg = create_os_error_message(error, show_traceback, using_user_site)
assert msg == expected

0 comments on commit d878727

Please sign in to comment.