From ee6f13e2f1473ece6643878e1d6f28ef41364242 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Thu, 5 Feb 2015 14:00:06 -0800 Subject: [PATCH] Display warning when 'SUDO_COMMAND' in os.environ Because using sudo with pip is often a mistake; often people either: - don't realize that `sudo` won't honor their activated virtualenv - are trying to manage packages in their system python, which can break systems and is often better left to the system package manager. --- pip/basecommand.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pip/basecommand.py b/pip/basecommand.py index 28e22b6731c..876d2434f39 100644 --- a/pip/basecommand.py +++ b/pip/basecommand.py @@ -4,6 +4,7 @@ import logging import os import sys +import textwrap import traceback import optparse import warnings @@ -211,6 +212,27 @@ def main(self, args): if options.exists_action: os.environ['PIP_EXISTS_ACTION'] = ' '.join(options.exists_action) + if 'SUDO_COMMAND' in os.environ: + logger.warning( + textwrap.dedent("""\ + ****************************************************** + Are you using sudo? Using pip with sudo can be tricky. + + If you're using a virtualenv: + + Sudo usually won't pass the environment variables + that are set by virtualenv's `activate` scripts, so + a `sudo pip install` may not run in the virtualenv. + + If you're installing to the system python: + + This is risky and can lead to system breakage. + Usually it is better to use your OS package manager + to manage your system python and to use pip in a + virtualenv or with the --user option. + ******************************************************""") + ) + if options.require_venv: # If a venv is required check if it can really be found if not running_under_virtualenv():