-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add a warning when run as root (e.g., sudo pip) #9394
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
Changes from all commits
ac4ad64
40d4b83
2873b17
420637f
856ab74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add a warning, discouraging the usage of pip as root, outside a virtual environment. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import logging | ||
import os | ||
import sys | ||
from functools import partial | ||
from optparse import Values | ||
from typing import Any, List, Optional, Tuple | ||
|
@@ -38,6 +39,7 @@ | |
TempDirectoryTypeRegistry, | ||
tempdir_kinds, | ||
) | ||
from pip._internal.utils.virtualenv import running_under_virtualenv | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -152,6 +154,35 @@ def handle_pip_version_check(self, options): | |
] | ||
|
||
|
||
def warn_if_run_as_root(): | ||
# type: () -> None | ||
"""Output a warning for sudo users on Unix. | ||
|
||
In a virtual environment, sudo pip still writes to virtualenv. | ||
On Windows, users may run pip as Administrator without issues. | ||
This warning only applies to Unix root users outside of virtualenv. | ||
""" | ||
if running_under_virtualenv(): | ||
return | ||
if not hasattr(os, "getuid"): | ||
return | ||
# On Windows, there are no "system managed" Python packages. Installing as | ||
# Administrator via pip is the correct way of updating system environments. | ||
# | ||
# We choose sys.platform over utils.compat.WINDOWS here to enable Mypy platform | ||
# checks: https://mypy.readthedocs.io/en/stable/common_issues.html | ||
if sys.platform == "win32" or sys.platform == "cygwin": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we doing this for cygwin? I'd have imagined that cygwin counts as a "system package manager" for the purposes of a cygwin build of Python. But having said that, I know very little about cygwin and I'm happy to assune it's OK unless cygwin users complain that they don't like it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’s been a looong while since I last used Cygwin, but IIRC it does not have a notion of superusers, so it makes sense to skip the checks in it. But it’s probably a good idea to not over-anticipate things and wait for complaints instead, since this is just a line of message anyway. |
||
return | ||
if sys.platform == "darwin" or sys.platform == "linux": | ||
if os.getuid() != 0: | ||
winsonluk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
logger.warning( | ||
"Running pip as root will break packages and permissions. " | ||
"You should install packages reliably by using venv: " | ||
"https://pip.pypa.io/warnings/venv" | ||
) | ||
|
||
|
||
def with_cleanup(func): | ||
# type: (Any) -> Any | ||
"""Decorator for common logic related to managing temporary | ||
|
Uh oh!
There was an error while loading. Please reload this page.