Repository: pypa/pipenv

Issue:
Pipenv clean fails if venv doesn't exist


<details><summary>$ python -m pipenv.help output</summary>
Pipenv version: `'11.7.1'`

Pipenv location: `'/usr/local/Cellar/pipenv/11.7.1/libexec/lib/python3.6/site-packages/pipenv'`

Python location: `'/usr/local/Cellar/pipenv/11.7.1/libexec/bin/python'`

Other Python installations in `PATH`:

  - `2.7`: `/usr/bin/python2.7`
  - `2.7`: `/usr/bin/python2.7`
  - `3.6`: `/usr/local/bin/python3.6m`
  - `3.6`: `/usr/local/Cellar/pipenv/11.7.1/libexec/bin/python3.6`
  - `3.6`: `/usr/local/bin/python3.6`
  - `3.6`: `/usr/local/bin/python3.6`

  - `3.6.4`: `/usr/local/Cellar/pipenv/11.7.1/libexec/bin/python`
  - `2.7.10`: `/usr/bin/python`
  - `3.6.4`: `/usr/local/Cellar/pipenv/11.7.1/libexec/bin/python3`
  - `3.6.4`: `/usr/local/bin/python3`
  - `3.6.4`: `/usr/local/bin/python3`

PEP 508 Information:

```
{'implementation_name': 'cpython',
 'implementation_version': '3.6.4',
 'os_name': 'posix',
 'platform_machine': 'x86_64',
 'platform_python_implementation': 'CPython',
 'platform_release': '17.4.0',
 'platform_system': 'Darwin',
 'platform_version': 'Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST '
                     '2017; root:xnu-4570.41.2~1/RELEASE_X86_64',
 'python_full_version': '3.6.4',
 'python_version': '3.6',
 'sys_platform': 'darwin'}
```

System environment variables:

  - `LDFLAGS`
  - `VIRTUALENVWRAPPER_SCRIPT`
  - `VIRTUALENVWRAPPER_PROJECT_FILENAME`
  - `TERM_PROGRAM`
  - `NVM_CD_FLAGS`
  - `TERM`
  - `SHELL`
  - `HISTSIZE`
  - `CPPFLAGS`
  - `TMPDIR`
  - `Apple_PubSub_Socket_Render`
  - `TERM_PROGRAM_VERSION`
  - `OLDPWD`
  - `TERM_SESSION_ID`
  - `LC_ALL`
  - `NVM_DIR`
  - `USER`
  - `CROSS_ACCOUNT_STACK_AWS_PROFILE_NAME_BBM_MASTER`
  - `COMMAND_MODE`
  - `SSH_AUTH_SOCK`
  - `__CF_USER_TEXT_ENCODING`
  - `FAB_COMPLETION_CACHE_TASKS`
  - `VIRTUAL_ENV`
  - `WORKON_HOME`
  - `PROJECT_HOME`
  - `VIRTUALENVWRAPPER_PYTHON`
  - `NPM_TOKEN`
  - `PATH`
  - `WERKZEUG_DEBUG_PIN`
  - `VIRTUALENVWRAPPER_HOOK_DIR`
  - `PWD`
  - `LANG`
  - `ITERM_PROFILE`
  - `XPC_FLAGS`
  - `PS1`
  - `FAB_COMPLETION_CACHED_TASKS_FILENAME`
  - `PYTEST_ADDOPTS`
  - `HISTCONTROL`
  - `XPC_SERVICE_NAME`
  - `SHLVL`
  - `HOME`
  - `COLORFGBG`
  - `CFLAGS`
  - `ITERM_SESSION_ID`
  - `LOGNAME`
  - `LC_CTYPE`
  - `PKG_CONFIG_PATH`
  - `PROMPT_COMMAND`
  - `VIRTUALENVWRAPPER_WORKON_CD`
  - `EXTRA_CFLAGS`
  - `SECURITYSESSIONID`
  - `EXTRA_LDFLAGS`
  - `COLORTERM`
  - `_`
  - `PIP_PYTHON_PATH`
  - `PYTHONUNBUFFERED`

Pipenv–specific environment variables:


Debug–specific environment variables:

  - `PATH`: `/usr/local/Cellar/pipenv/11.7.1/libexec/bin:/Users/craig/.yarn/bin:/Users/craig/.local/bin:/usr/local/opt/openssl/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin`
  - `SHELL`: `/bin/bash`
  - `LANG`: `en_GB.UTF-8`
  - `PWD`: `/tmp`
  - `VIRTUAL_ENV`: `/usr/local/Cellar/pipenv/11.7.1/libexec`


---------------------------

Contents of `Pipfile` ('/private/tmp/Pipfile'):

```toml
[[source]]

url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"


[packages]

requests = "*"


[dev-packages]



[requires]

python_version = "3.6"

```


Contents of `Pipfile.lock` ('/private/tmp/Pipfile.lock'):

```json
{
    "_meta": {
        "hash": {
            "sha256": "33a0ec7c8e3bae6f62dd618f847de92ece20e2bd4efb496927e2524b9c7b8df8"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.6"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.python.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "certifi": {
            "hashes": [
                "sha256:14131608ad2fd56836d33a71ee60fa1c82bc9d2c8d98b7bdbc631fe1b3cd1296",
                "sha256:edbc3f203427eef571f79a7692bb160a2b0f7ccaa31953e99bd17e307cf63f7d"
            ],
            "version": "==2018.1.18"
        },
        "chardet": {
            "hashes": [
                "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",
                "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"
            ],
            "version": "==3.0.4"
        },
        "idna": {
            "hashes": [
                "sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f",
                "sha256:8c7309c718f94b3a625cb648ace320157ad16ff131ae0af362c9f21b80ef6ec4"
            ],
            "version": "==2.6"
        },
        "requests": {
            "hashes": [
                "sha256:6a1b267aa90cac58ac3a765d067950e7dbbf75b1da07e895d1f594193a40a38b",
                "sha256:9c443e7324ba5b85070c4a818ade28bfabedf16ea10206da1132edaa6dda237e"
            ],
            "index": "pypi",
            "version": "==2.18.4"
        },
        "urllib3": {
            "hashes": [
                "sha256:06330f386d6e4b195fbfc736b297f58c5a892e4440e54d294d7004e3a9bbea1b",
                "sha256:cc44da8e1145637334317feebd728bd869a35285b93cbb4cca2577da7e62db4f"
            ],
            "version": "==1.22"
        }
    },
    "develop": {}
}

```
</details>
------------

##### Expected result

`pipenv clean` exist successfully. Virtualenv is created.

##### Actual result

```
craig@amogato2:/tmp
$ pipenv clean
Creating a virtualenv for this project…
Using /usr/local/bin/python3.6m (3.6.4) to create virtualenv…
⠋Running virtualenv with interpreter /usr/local/bin/python3.6m
Using base prefix '/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/craig/.virtualenvs/tmp-agwWamBd/bin/python3.6
Also creating executable in /Users/craig/.virtualenvs/tmp-agwWamBd/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /Users/craig/.virtualenvs/tmp-agwWamBd
Traceback (most recent call last):
  File "/usr/local/Cellar/pipenv/11.7.1/libexec/bin/pipenv", line 11, in <module>
    load_entry_point('pipenv==11.7.1', 'console_scripts', 'pipenv')()
  File "/usr/local/Cellar/pipenv/11.7.1/libexec/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/Cellar/pipenv/11.7.1/libexec/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/Cellar/pipenv/11.7.1/libexec/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/Cellar/pipenv/11.7.1/libexec/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/Cellar/pipenv/11.7.1/libexec/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/Cellar/pipenv/11.7.1/libexec/lib/python3.6/site-packages/pipenv/vendor/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/Cellar/pipenv/11.7.1/libexec/lib/python3.6/site-packages/pipenv/cli.py", line 459, in clean
    ctx=ctx, three=three, python=python, dry_run=dry_run, verbose=verbose
  File "/usr/local/Cellar/pipenv/11.7.1/libexec/lib/python3.6/site-packages/pipenv/core.py", line 2451, in do_clean
    r = get_requirement(installed)
  File "/usr/local/Cellar/pipenv/11.7.1/libexec/lib/python3.6/site-packages/pipenv/utils.py", line 128, in get_requirement
    req = [r for r in requirements.parse(dep)][0]
IndexError: list index out of range
```

##### Steps to replicate

```
$ cd /tmp
$ mkdir clean-bug
$ cd clean-bug
$ pipenv install requests
$ pipenv --rm
$ pipenv clean
```



Discussion:
thanks for the report!

Resolving patch:
diff --git a/pipenv/core.py b/pipenv/core.py
--- a/pipenv/core.py
+++ b/pipenv/core.py
@@ -2523,12 +2523,12 @@ def do_clean(
     # Ensure that virtualenv is available.
     ensure_project(three=three, python=python, validate=False)
     ensure_lockfile()
-    installed_packages = delegator.run(
+    installed_packages = filter(None, delegator.run(
         '{0} freeze'.format(which('pip'))
     ).out.strip(
     ).split(
         '\n'
-    )
+    ))
     installed_package_names = []
     for installed in installed_packages:
         r = get_requirement(installed)


Test patch:
diff --git a/tests/test_pipenv.py b/tests/test_pipenv.py
--- a/tests/test_pipenv.py
+++ b/tests/test_pipenv.py
@@ -1051,3 +1051,9 @@ def test_install_local_file_collision(self, pypi):
             assert target_package in p.pipfile['packages']
             assert p.pipfile['packages'][target_package] == '*'
             assert target_package in p.lockfile['default']
+
+    @pytest.mark.clean
+    def test_clean_on_empty_venv(self, pypi):
+        with PipenvInstance(pypi=pypi) as p:
+            c = p.pipenv('clean')
+            assert c.return_code == 0