# Buggy code
def pip_install(
    package_name=None,
    r=None,
    allow_global=False,
    ignore_hashes=False,
    no_deps=True,
    verbose=False,
    block=True,
    index=None,
    pre=False,
):
    if verbose:
        click.echo(
            crayons.normal("Installing {0!r}".format(package_name), bold=True), err=True
        )

    # Create files for hash mode.
    if (not ignore_hashes) and (r is None):
        r = tempfile.mkstemp(prefix="pipenv-", suffix="-requirement.txt")[1]
        with open(r, "w") as f:
            f.write(package_name)

    # Install dependencies when a package is a VCS dependency.
    if get_requirement(package_name.split("--hash")[0].split("--trusted-host")[0]).vcs:
        no_deps = False

        # Don't specify a source directory when using --system.
        if not allow_global and ("PIP_SRC" not in os.environ):
            src = "--src {0}".format(project.virtualenv_src_location)
        else:
            src = ""
    else:
        src = ""

    # Try installing for each source in project.sources.
    if index:
        sources = [{"url": index}]
    else:
        sources = project.sources

    for source in sources:
        if r:
            install_reqs = " -r {0}".format(r)
        elif package_name.startswith("-e "):
            install_reqs = ' -e "{0}"'.format(package_name.split("-e ")[1])
        else:
            install_reqs = ' "{0}"'.format(package_name)

        # Skip hash-checking mode, when appropriate.
        if r:
            with open(r) as f:
                if "--hash" not in f.read():
                    ignore_hashes = True
        else:
            if "--hash" not in install_reqs:
                ignore_hashes = True

        verbose_flag = "--verbose" if verbose else ""

        if not ignore_hashes:
            install_reqs += " --require-hashes"

        no_deps = "--no-deps" if no_deps else ""
        pre = "--pre" if pre else ""

        quoted_pip = which_pip(allow_global=allow_global)
        quoted_pip = shellquote(quoted_pip)

        pip_command = "{0} install {4} {5} {6} {3} {1} {2} --exists-action w".format(
            quoted_pip,
            install_reqs,
            " ".join(prepare_pip_source_args([source])),
            no_deps,
            pre,
            src,
            verbose_flag,
        )

        if verbose:
            click.echo("$ {0}".format(pip_command), err=True)

        c = delegator.run(pip_command, block=block)
        if c.return_code == 0:
            break

    # Return the result of the first one that runs ok, or the last one that didn't work.
    return c


# Traceback (pip.exceptions.RequirementsFileParseError)
$ pipenv install --verbose
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Usage: pipenv [options]

Traceback (most recent call last):
File "/Users/aleskocur-rubicoin/Library/Python/2.7/bin/pipenv", line 9, in <module>
load_entry_point('pipenv==8.3.2', 'console_scripts', 'pipenv')()
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/vendor/click/core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/vendor/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/vendor/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/vendor/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/vendor/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/cli.py", line 1866, in install
do_init(dev=dev, allow_global=system, ignore_pipfile=ignore_pipfile, system=system, skip_lock=skip_lock, verbose=verbose, concurrent=concurrent, deploy=deploy, pre=pre)
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/cli.py", line 1330, in do_init
do_lock(system=system, pre=pre)
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/cli.py", line 1121, in do_lock
pre=pre
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/utils.py", line 426, in resolve_deps
constraint = [c for c in pip.req.parse_requirements(t, session=pip._vendor.requests)][0]
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/patched/pip/req/req_file.py", line 93, in parse_requirements
for req in req_iter:
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/patched/pip/req/req_file.py", line 140, in process_line
opts, _ = parser.parse_args(shlex.split(options_str), defaults)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/optparse.py", line 1402, in parse_args
self.error(str(err))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/optparse.py", line 1584, in error
self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
File "/Users/aleskocur-rubicoin/Library/Python/2.7/lib/python/site-packages/pipenv/patched/pip/req/req_file.py", line 284, in parser_exit
raise RequirementsFileParseError(msg)
pip.exceptions.RequirementsFileParseError: pipenv: error: no such option: --upgrade

# Fixed code
def pip_install(
    package_name=None,
    r=None,
    allow_global=False,
    ignore_hashes=False,
    no_deps=True,
    verbose=False,
    block=True,
    index=None,
    pre=False,
):
    if verbose:
        click.echo(
            crayons.normal("Installing {0!r}".format(package_name), bold=True), err=True
        )

    # Create files for hash mode.
    if (not ignore_hashes) and (r is None):
        r = tempfile.mkstemp(prefix="pipenv-", suffix="-requirement.txt")[1]
        with open(r, "w") as f:
            f.write(package_name)

    # Install dependencies when a package is a VCS dependency.
    try:
        req = get_requirement(
            package_name.split("--hash")[0].split("--trusted-host")[0]
        ).vcs
    except (pip._vendor.pyparsing.ParseException, ValueError) as e:
        click.echo("{0}: {1}".format(crayons.red("WARNING"), e), err=True)
        click.echo(
            "{0}... You will have to reinstall any packages that failed to install.".format(
                crayons.red("ABORTING INSTALL")
            ),
            err=True,
        )
        click.echo(
            "You may have to manually run {0} when you are finished.".format(
                crayons.normal("pipenv lock", bold=True)
            )
        )
        sys.exit(1)
    if req:
        no_deps = False

        # Don't specify a source directory when using --system.
        if not allow_global and ("PIP_SRC" not in os.environ):
            src = "--src {0}".format(project.virtualenv_src_location)
        else:
            src = ""
    else:
        src = ""

    # Try installing for each source in project.sources.
    if index:
        sources = [{"url": index}]
    else:
        sources = project.sources

    for source in sources:
        if r:
            install_reqs = " -r {0}".format(r)
        elif package_name.startswith("-e "):
            install_reqs = ' -e "{0}"'.format(package_name.split("-e ")[1])
        else:
            install_reqs = ' "{0}"'.format(package_name)

        # Skip hash-checking mode, when appropriate.
        if r:
            with open(r) as f:
                if "--hash" not in f.read():
                    ignore_hashes = True
        else:
            if "--hash" not in install_reqs:
                ignore_hashes = True

        verbose_flag = "--verbose" if verbose else ""

        if not ignore_hashes:
            install_reqs += " --require-hashes"

        no_deps = "--no-deps" if no_deps else ""
        pre = "--pre" if pre else ""

        quoted_pip = which_pip(allow_global=allow_global)
        quoted_pip = shellquote(quoted_pip)

        pip_command = "{0} install {4} {5} {6} {3} {1} {2} --exists-action w".format(
            quoted_pip,
            install_reqs,
            " ".join(prepare_pip_source_args([source])),
            no_deps,
            pre,
            src,
            verbose_flag,
        )

        if verbose:
            click.echo("$ {0}".format(pip_command), err=True)

        c = delegator.run(pip_command, block=block)
        if c.return_code == 0:
            break

    # Return the result of the first one that runs ok, or the last one that didn't work.
    return c