Skip to content

Commit 4e835c0

Browse files
authored
Merge pull request #9442 from permission-denied-hg
2 parents 2a3e4f9 + b88de7d commit 4e835c0

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

docs/html/cli/pip_freeze.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,21 @@ Examples
7272
7373
env1\bin\python -m pip freeze > requirements.txt
7474
env2\bin\python -m pip install -r requirements.txt
75+
76+
77+
Fixing "Permission denied:" errors
78+
==================================
79+
80+
The purpose of this section of documentation is to provide practical
81+
suggestions to users seeing a `"Permission denied" error <https://github.com/pypa/pip/issues/8418>`__ on ``pip freeze``.
82+
83+
This error occurs, for instance, when the command is installed only for another
84+
user, and the current user doesn't have the permission to execute the other
85+
user's command.
86+
87+
To solve that issue, you can try one of the followings:
88+
89+
- Install the command for yourself (e.g. in your home directory).
90+
- Ask the system admin to allow this command for all users.
91+
- Check and correct the PATH variable of your own environment.
92+
- Check the `ACL (Access-Control List) <https://en.wikipedia.org/wiki/Access-control_list>`_ for this command.

news/8418.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``pip freeze`` permission denied error in order to display an understandable error message and offer solutions.

news/8418.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a section in the documentation to suggest solutions to the ``pip freeze`` permission denied issue.

src/pip/_internal/vcs/versioncontrol.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,18 @@ def run_command(
684684
raise BadCommand(
685685
f'Cannot find command {cls.name!r} - do you have '
686686
f'{cls.name!r} installed and in your PATH?')
687+
except PermissionError:
688+
# errno.EACCES = Permission denied
689+
# This error occurs, for instance, when the command is installed
690+
# only for another user. So, the current user don't have
691+
# permission to call the other user command.
692+
raise BadCommand(
693+
f"No permission to execute {cls.name!r} - install it "
694+
f"locally, globally (ask admin), or check your PATH. "
695+
f"See possible solutions at "
696+
f"https://pip.pypa.io/en/latest/reference/pip_freeze/"
697+
f"#fixing-permission-denied."
698+
)
687699

688700
@classmethod
689701
def is_repository_directory(cls, path):

tests/unit/test_vcs.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,27 @@ def test_version_control__get_url_rev_and_auth__no_revision(url):
303303
assert 'an empty revision (after @)' in str(excinfo.value)
304304

305305

306+
@pytest.mark.parametrize("vcs_cls", [Bazaar, Git, Mercurial, Subversion])
307+
@pytest.mark.parametrize(
308+
"exc_cls, msg_re",
309+
[
310+
(FileNotFoundError, r"Cannot find command '{name}'"),
311+
(PermissionError, r"No permission to execute '{name}'"),
312+
],
313+
ids=["FileNotFoundError", "PermissionError"],
314+
)
315+
def test_version_control__run_command__fails(vcs_cls, exc_cls, msg_re):
316+
"""
317+
Test that ``VersionControl.run_command()`` raises ``BadCommand``
318+
when the command is not found or when the user have no permission
319+
to execute it. The error message must contains the command name.
320+
"""
321+
with patch("pip._internal.vcs.versioncontrol.call_subprocess") as call:
322+
call.side_effect = exc_cls
323+
with pytest.raises(BadCommand, match=msg_re.format(name=vcs_cls.name)):
324+
vcs_cls.run_command([])
325+
326+
306327
@pytest.mark.parametrize('url, expected', [
307328
# Test http.
308329
('bzr+http://bzr.myproject.org/MyProject/trunk/#egg=MyProject',

0 commit comments

Comments
 (0)