From 4a1c7b891f70688f756ab0fa75c0d264df6c3358 Mon Sep 17 00:00:00 2001 From: Pradyun Gedam Date: Sun, 15 Apr 2018 19:55:31 +0530 Subject: [PATCH 1/3] Print Better Error Messages on EnvironmenErrors while running pip install. This fixes various issues with the current logic and splits out the message generation into a separate unit-test-able function. Improvements: - Does not not-print any information in some cases - Mentions original error message, or entire traceback if verbose. - In case of permission/access errors, suggests the user to use --user and/or check permissions --- src/pip/_internal/commands/install.py | 65 ++++++++++++++++++++------- 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/src/pip/_internal/commands/install.py b/src/pip/_internal/commands/install.py index f28c6ea5ae7..bea97229816 100644 --- a/src/pip/_internal/commands/install.py +++ b/src/pip/_internal/commands/install.py @@ -358,25 +358,14 @@ def run(self, options, args): installed = ' '.join(items) if installed: logger.info('Successfully installed %s', installed) - except EnvironmentError as e: - message_parts = [] - - user_option_part = "Consider using the `--user` option" - permissions_part = "Check the permissions" - - if e.errno == errno.EPERM: - if not options.use_user_site: - message_parts.extend([ - user_option_part, " or ", - permissions_part.lower(), - ]) - else: - message_parts.append(permissions_part) - message_parts.append("\n") + except EnvironmentError as error: + show_traceback = (self.verbosity >= 1) - logger.error( - "".join(message_parts), exc_info=(self.verbosity > 1) + message = create_env_error_message( + error, show_traceback, options.use_user_site, ) + logger.error(message, exc_info=show_traceback) + return ERROR except PreviousBuildDirError: options.no_clean = True @@ -475,3 +464,45 @@ def _warn_about_conflicts(self, to_install): def get_lib_location_guesses(*args, **kwargs): scheme = distutils_scheme('', *args, **kwargs) return [scheme['purelib'], scheme['platlib']] + + +def create_env_error_message(error, show_traceback, using_user_site): + """Format an error message for an EnvironmentError + + It may occur anytime during the execution of the install command. + """ + parts = [] + + # Mention the error if we are not going to show a traceback + parts.append("Could not install packages due to an EnvironmentError") + if not show_traceback: + parts.append(": ") + parts.append(str(error)) + else: + parts.append(".") + + # Spilt the error indication from a helper message (if any) + parts[-1] += "\n" + + # Suggest useful actions to the user: + # (1) using user site-packages or (2) verifying the permissions + if error.errno == errno.EACCES: + user_option_part = "Consider using the `--user` option" + permissions_part = "Check the permissions" + + if not using_user_site: + parts.extend([ + user_option_part, " or ", + permissions_part.lower(), + ]) + else: + parts.append(permissions_part) + parts.append(".\n") + + # If we won't show a traceback, tell the user how to get it. + if not show_traceback: + parts.append( + "To view error traceback, increase verbosity by passing --verbose." + ) + + return "".join(parts).strip() + "\n" From fa1219fefeb810329a71ba6655088dd26a114f5f Mon Sep 17 00:00:00 2001 From: Pradyun Gedam Date: Sun, 15 Apr 2018 19:57:21 +0530 Subject: [PATCH 2/3] :newspaper: --- news/5237.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/5237.bugfix diff --git a/news/5237.bugfix b/news/5237.bugfix new file mode 100644 index 00000000000..c12e576b982 --- /dev/null +++ b/news/5237.bugfix @@ -0,0 +1 @@ +Fix and improve error message when EnvironmentError occurs during installation. From 3e7a66c070400b241b7498af8b1a09b99a0c14a2 Mon Sep 17 00:00:00 2001 From: Pradyun Gedam Date: Tue, 17 Apr 2018 14:55:08 +0530 Subject: [PATCH 3/3] Don't advice about --verbose (it'ss obvious) --- src/pip/_internal/commands/install.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/pip/_internal/commands/install.py b/src/pip/_internal/commands/install.py index bea97229816..9138683acdd 100644 --- a/src/pip/_internal/commands/install.py +++ b/src/pip/_internal/commands/install.py @@ -499,10 +499,4 @@ def create_env_error_message(error, show_traceback, using_user_site): parts.append(permissions_part) parts.append(".\n") - # If we won't show a traceback, tell the user how to get it. - if not show_traceback: - parts.append( - "To view error traceback, increase verbosity by passing --verbose." - ) - return "".join(parts).strip() + "\n"