Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Error Messages on EnvironmentErrors when installing #5239

Merged
merged 3 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/5237.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix and improve error message when EnvironmentError occurs during installation.
59 changes: 42 additions & 17 deletions src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -475,3 +464,39 @@ 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")

return "".join(parts).strip() + "\n"