Repository: pypa/pipenv

Issue:
pipenv always create new virtualenv if PIPENV_SITE_PACKAGES=1 is set
### Issue description

When the environment variable `PIPENV_SITE_PACKAGES` is set, pipenv always recreate new virtualenv on each invoke of pipenv even if it is not a `pipenv install` command.

### Expected result

- `pipenv run` or `pipenv shell` should use existing virtualenv instead of creating a new one.
- `PIPENV_SITE_PACKAGES` should only affect `pipenv install` command.

### Actual result

```
root@9e08ac3143d2:/tmp# pipenv && pipenv && pipenv
Warning: the environment variable LANG is not set!
We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
Creating a virtualenv for this project...
Pipfile: /tmp/Pipfile
Using /usr/bin/python3.8 (3.8.5) to create virtualenv...
Making site-packages available...
⠼ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 254ms
  creator CPython3Posix(dest=/root/.local/share/virtualenvs/tmp-XVr6zr33, clear=False, global=True)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/tmp-XVr6zr33
Creating a Pipfile for this project...
Virtualenv already exists!
Removing existing virtualenv...
Warning: the environment variable LANG is not set!
We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
Creating a virtualenv for this project...
Pipfile: /tmp/Pipfile
Using /usr/bin/python3.8 (3.8.5) to create virtualenv...
Making site-packages available...
⠼ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 235ms
  creator CPython3Posix(dest=/root/.local/share/virtualenvs/tmp-XVr6zr33, clear=False, global=True)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/tmp-XVr6zr33
Virtualenv already exists!
Removing existing virtualenv...
Warning: the environment variable LANG is not set!
We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
Creating a virtualenv for this project...
Pipfile: /tmp/Pipfile
Using /usr/bin/python3.8 (3.8.5) to create virtualenv...
Making site-packages available...
⠼ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 239ms
  creator CPython3Posix(dest=/root/.local/share/virtualenvs/tmp-XVr6zr33, clear=False, global=True)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/tmp-XVr6zr33
root@9e08ac3143d2:/tmp#
```

### Steps to replicate

- `docker run -it ubuntu bash`
- Inside docker: `apt update && apt install -y python3 python3-pip && pip3 install pipenv && export PIPENV_SITE_PACKAGES=1`
- run any pipenv command, in the example I use `pipenv && pipenv && pipenv`

-------------------------------------------------------------------------------

</summary>

Pipenv version: `'2020.11.15'`

Pipenv location: `'/usr/local/lib/python3.8/dist-packages/pipenv'`

Python location: `'/usr/bin/python3'`

Python installations found:

  - `3.8.5`: `/usr/bin/python3.8`
  - `3.8.5`: `/usr/bin/python3`
  - `3.8.5`: `/bin/python3.8`
  - `3.8.5`: `/bin/python3`

PEP 508 Information:

```
{'implementation_name': 'cpython',
 'implementation_version': '3.8.5',
 'os_name': 'posix',
 'platform_machine': 'x86_64',
 'platform_python_implementation': 'CPython',
 'platform_release': '5.4.39-linuxkit',
 'platform_system': 'Linux',
 'platform_version': '#1 SMP Fri May 8 23:03:06 UTC 2020',
 'python_full_version': '3.8.5',
 'python_version': '3.8',
 'sys_platform': 'linux'}
```

System environment variables:

  - `HOSTNAME`
  - `PWD`
  - `HOME`
  - `LS_COLORS`
  - `TERM`
  - `PIPENV_SITE_PACKAGES`
  - `SHLVL`
  - `PATH`
  - `OLDPWD`
  - `_`
  - `LC_CTYPE`
  - `PIP_DISABLE_PIP_VERSION_CHECK`
  - `PYTHONDONTWRITEBYTECODE`
  - `PIP_SHIMS_BASE_MODULE`
  - `PIP_PYTHON_PATH`
  - `PYTHONFINDER_IGNORE_UNSUPPORTED`

Pipenv–specific environment variables:

 - `PIPENV_SITE_PACKAGES`: `1`

Debug–specific environment variables:

  - `PATH`: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin`
  - `PWD`: `/tmp`


---------------------------

Contents of `Pipfile` ('/tmp/Pipfile'):

```toml
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.8"

```

</details>
pipenv always create new virtualenv if PIPENV_SITE_PACKAGES=1 is set
### Issue description

When the environment variable `PIPENV_SITE_PACKAGES` is set, pipenv always recreate new virtualenv on each invoke of pipenv even if it is not a `pipenv install` command.

### Expected result

- `pipenv run` or `pipenv shell` should use existing virtualenv instead of creating a new one.
- `PIPENV_SITE_PACKAGES` should only affect `pipenv install` command.

### Actual result

```
root@9e08ac3143d2:/tmp# pipenv && pipenv && pipenv
Warning: the environment variable LANG is not set!
We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
Creating a virtualenv for this project...
Pipfile: /tmp/Pipfile
Using /usr/bin/python3.8 (3.8.5) to create virtualenv...
Making site-packages available...
⠼ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 254ms
  creator CPython3Posix(dest=/root/.local/share/virtualenvs/tmp-XVr6zr33, clear=False, global=True)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/tmp-XVr6zr33
Creating a Pipfile for this project...
Virtualenv already exists!
Removing existing virtualenv...
Warning: the environment variable LANG is not set!
We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
Creating a virtualenv for this project...
Pipfile: /tmp/Pipfile
Using /usr/bin/python3.8 (3.8.5) to create virtualenv...
Making site-packages available...
⠼ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 235ms
  creator CPython3Posix(dest=/root/.local/share/virtualenvs/tmp-XVr6zr33, clear=False, global=True)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/tmp-XVr6zr33
Virtualenv already exists!
Removing existing virtualenv...
Warning: the environment variable LANG is not set!
We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
Creating a virtualenv for this project...
Pipfile: /tmp/Pipfile
Using /usr/bin/python3.8 (3.8.5) to create virtualenv...
Making site-packages available...
⠼ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 239ms
  creator CPython3Posix(dest=/root/.local/share/virtualenvs/tmp-XVr6zr33, clear=False, global=True)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/tmp-XVr6zr33
root@9e08ac3143d2:/tmp#
```

### Steps to replicate

- `docker run -it ubuntu bash`
- Inside docker: `apt update && apt install -y python3 python3-pip && pip3 install pipenv && export PIPENV_SITE_PACKAGES=1`
- run any pipenv command, in the example I use `pipenv && pipenv && pipenv`

-------------------------------------------------------------------------------

</summary>

Pipenv version: `'2020.11.15'`

Pipenv location: `'/usr/local/lib/python3.8/dist-packages/pipenv'`

Python location: `'/usr/bin/python3'`

Python installations found:

  - `3.8.5`: `/usr/bin/python3.8`
  - `3.8.5`: `/usr/bin/python3`
  - `3.8.5`: `/bin/python3.8`
  - `3.8.5`: `/bin/python3`

PEP 508 Information:

```
{'implementation_name': 'cpython',
 'implementation_version': '3.8.5',
 'os_name': 'posix',
 'platform_machine': 'x86_64',
 'platform_python_implementation': 'CPython',
 'platform_release': '5.4.39-linuxkit',
 'platform_system': 'Linux',
 'platform_version': '#1 SMP Fri May 8 23:03:06 UTC 2020',
 'python_full_version': '3.8.5',
 'python_version': '3.8',
 'sys_platform': 'linux'}
```

System environment variables:

  - `HOSTNAME`
  - `PWD`
  - `HOME`
  - `LS_COLORS`
  - `TERM`
  - `PIPENV_SITE_PACKAGES`
  - `SHLVL`
  - `PATH`
  - `OLDPWD`
  - `_`
  - `LC_CTYPE`
  - `PIP_DISABLE_PIP_VERSION_CHECK`
  - `PYTHONDONTWRITEBYTECODE`
  - `PIP_SHIMS_BASE_MODULE`
  - `PIP_PYTHON_PATH`
  - `PYTHONFINDER_IGNORE_UNSUPPORTED`

Pipenv–specific environment variables:

 - `PIPENV_SITE_PACKAGES`: `1`

Debug–specific environment variables:

  - `PATH`: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin`
  - `PWD`: `/tmp`


---------------------------

Contents of `Pipfile` ('/tmp/Pipfile'):

```toml
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.8"

```

</details>


Discussion:
Same +1
Currently passing `--site-packages`, the equivalent of setting `PIPENV_SITE_PACKAGES`, will be regarded as the user wants to change the virtualenv properties. So it is a one-pass flag rather than a persistent setting, but it should be easy to fix:
https://github.com/pypa/pipenv/blob/47d8b96e10e462169cb9d3e719318565514408ad/pipenv%2Fcore.py#L519-L546

Any interest in submitting a PR?
I would like to work on this for my first issue. If you agree then I will start working on it.
> I would like to work on this for my first issue. If you agree then I will start working on it.

Go ahead. actually you don't need to ask for permission.
I want to ask some questions about how pipenv should behave with `PIPENV_SITE_PACKAGES=1.`

- It should create a new virtual env ONLY one time? Or should it never create a virtual env? 
- If the virtual env is already created, should it show a message about the virtual env has already been created?
Are there any plans to fix this?
I have tried to run the code on my computer. On running the command "pipenv --three" it shows that virtual environment already exists and then it removes the existing virtual environment and create new one. My doubt is that we have to remove this functionality and add  if any virtual environment exists it should use that?
Same +1
Currently passing `--site-packages`, the equivalent of setting `PIPENV_SITE_PACKAGES`, will be regarded as the user wants to change the virtualenv properties. So it is a one-pass flag rather than a persistent setting, but it should be easy to fix:
https://github.com/pypa/pipenv/blob/47d8b96e10e462169cb9d3e719318565514408ad/pipenv%2Fcore.py#L519-L546

Any interest in submitting a PR?
I would like to work on this for my first issue. If you agree then I will start working on it.
> I would like to work on this for my first issue. If you agree then I will start working on it.

Go ahead. actually you don't need to ask for permission.
I want to ask some questions about how pipenv should behave with `PIPENV_SITE_PACKAGES=1.`

- It should create a new virtual env ONLY one time? Or should it never create a virtual env? 
- If the virtual env is already created, should it show a message about the virtual env has already been created?
Are there any plans to fix this?
I have tried to run the code on my computer. On running the command "pipenv --three" it shows that virtual environment already exists and then it removes the existing virtual environment and create new one. My doubt is that we have to remove this functionality and add  if any virtual environment exists it should use that?

Resolving patch:
diff --git a/pipenv/core.py b/pipenv/core.py
--- a/pipenv/core.py
+++ b/pipenv/core.py
@@ -483,11 +483,11 @@ def abort():
         # about, so confirm first.
         if "VIRTUAL_ENV" in os.environ:
             if not (
-                project.s.PIPENV_YES or click.confirm("Remove existing virtualenv?", default=True)
+                project.s.PIPENV_YES or click.confirm("Use existing virtualenv?", default=True)
             ):
                 abort()
         click.echo(
-            crayons.normal(fix_utf8("Removing existing virtualenv..."), bold=True), err=True
+            crayons.normal(fix_utf8("Using existing virtualenv..."), bold=True), err=True
         )
         # Remove the virtualenv.
         cleanup_virtualenv(project, bare=True)
@@ -517,12 +517,14 @@ def ensure_project(
     """Ensures both Pipfile and virtualenv exist for the project."""
 
     # Automatically use an activated virtualenv.
-    if project.s.PIPENV_USE_SYSTEM:
-        system = True
+    if project.s.PIPENV_USE_SYSTEM or project.virtualenv_exists:
+        system_or_exists = True
+    else:
+        system_or_exists = system # default to False
     if not project.pipfile_exists and deploy:
         raise exceptions.PipfileNotFound
     # Skip virtualenv creation when --system was used.
-    if not system:
+    if not system_or_exists:
         ensure_virtualenv(
             project,
             three=three,
@@ -563,7 +565,7 @@ def ensure_project(
                         raise exceptions.DeployException
     # Ensure the Pipfile exists.
     ensure_pipfile(
-        project, validate=validate, skip_requirements=skip_requirements, system=system
+        project, validate=validate, skip_requirements=skip_requirements, system=system_or_exists
     )
 
 


Test patch: