From ccf22d5ae2620c50640a9a45bb68ed45b0037fc3 Mon Sep 17 00:00:00 2001 From: Allen Li Date: Wed, 22 Nov 2017 00:18:37 -0800 Subject: [PATCH] Prompt for confirmation before replacing existing virtualenv Fixes https://github.com/kennethreitz/pipenv/issues/997 --- pipenv/cli.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pipenv/cli.py b/pipenv/cli.py index c9f97e5c..0c1b2397 100644 --- a/pipenv/cli.py +++ b/pipenv/cli.py @@ -544,6 +544,9 @@ def ensure_python(three=None, python=None): def ensure_virtualenv(three=None, python=None, site_packages=False): """Creates a virtualenv, if one doesn't exist.""" + def abort(): + sys.exit(1) + global USING_DEFAULT_PYTHON if not project.virtualenv_exists: @@ -580,6 +583,12 @@ def ensure_virtualenv(three=None, python=None, site_packages=False): ensure_python(three=three, python=python) click.echo(crayons.red('Virtualenv already exists!'), err=True) + # If VIRTUAL_ENV is set, there is a possibility that we are + # going to remove the active virtualenv that the user cares + # about, so confirm first. + if 'VIRTUAL_ENV' in os.environ: + if not (PIPENV_YES or click.confirm('Remove existing virtualenv?', default=True)): + abort() click.echo(crayons.normal(u'Removing existing virtualenv…', bold=True), err=True) # Remove the virtualenv.