Skip to content

Commit 9875055

Browse files
authored
Merge pull request #5239 from pradyunsg/fix/5237
Improve Error Messages on EnvironmentErrors when installing
2 parents ea1319d + 3e7a66c commit 9875055

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

news/5237.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix and improve error message when EnvironmentError occurs during installation.

src/pip/_internal/commands/install.py

+42-17
Original file line numberDiff line numberDiff line change
@@ -358,25 +358,14 @@ def run(self, options, args):
358358
installed = ' '.join(items)
359359
if installed:
360360
logger.info('Successfully installed %s', installed)
361-
except EnvironmentError as e:
362-
message_parts = []
363-
364-
user_option_part = "Consider using the `--user` option"
365-
permissions_part = "Check the permissions"
366-
367-
if e.errno == errno.EPERM:
368-
if not options.use_user_site:
369-
message_parts.extend([
370-
user_option_part, " or ",
371-
permissions_part.lower(),
372-
])
373-
else:
374-
message_parts.append(permissions_part)
375-
message_parts.append("\n")
361+
except EnvironmentError as error:
362+
show_traceback = (self.verbosity >= 1)
376363

377-
logger.error(
378-
"".join(message_parts), exc_info=(self.verbosity > 1)
364+
message = create_env_error_message(
365+
error, show_traceback, options.use_user_site,
379366
)
367+
logger.error(message, exc_info=show_traceback)
368+
380369
return ERROR
381370
except PreviousBuildDirError:
382371
options.no_clean = True
@@ -475,3 +464,39 @@ def _warn_about_conflicts(self, to_install):
475464
def get_lib_location_guesses(*args, **kwargs):
476465
scheme = distutils_scheme('', *args, **kwargs)
477466
return [scheme['purelib'], scheme['platlib']]
467+
468+
469+
def create_env_error_message(error, show_traceback, using_user_site):
470+
"""Format an error message for an EnvironmentError
471+
472+
It may occur anytime during the execution of the install command.
473+
"""
474+
parts = []
475+
476+
# Mention the error if we are not going to show a traceback
477+
parts.append("Could not install packages due to an EnvironmentError")
478+
if not show_traceback:
479+
parts.append(": ")
480+
parts.append(str(error))
481+
else:
482+
parts.append(".")
483+
484+
# Spilt the error indication from a helper message (if any)
485+
parts[-1] += "\n"
486+
487+
# Suggest useful actions to the user:
488+
# (1) using user site-packages or (2) verifying the permissions
489+
if error.errno == errno.EACCES:
490+
user_option_part = "Consider using the `--user` option"
491+
permissions_part = "Check the permissions"
492+
493+
if not using_user_site:
494+
parts.extend([
495+
user_option_part, " or ",
496+
permissions_part.lower(),
497+
])
498+
else:
499+
parts.append(permissions_part)
500+
parts.append(".\n")
501+
502+
return "".join(parts).strip() + "\n"

0 commit comments

Comments
 (0)