# Buggy code
def _needs_reinstall(self, setupdir, action):
    setup_py = setupdir.join("setup.py")
    setup_cfg = setupdir.join("setup.cfg")
    args = [self.envconfig.envpython, str(setup_py), "--name"]
    env = self._getenv()
    output = action.popen(args, cwd=setupdir, redirect=False, returnout=True, env=env)
    name = output.strip()
    args = [self.envconfig.envpython, "-c", "import sys; print(sys.path)"]
    out = action.popen(args, redirect=False, returnout=True, env=env)
    try:
        sys_path = ast.literal_eval(out.strip())
    except SyntaxError:
        sys_path = []
    egg_info_fname = ".".join((name, "egg-info"))
    for d in reversed(sys_path):
        egg_info = py.path.local(d).join(egg_info_fname)
        if egg_info.check():
            break
    else:
        return True
    needs_reinstall = any(
        conf_file.check() and conf_file.mtime() > egg_info.mtime()
        for conf_file in (setup_py, setup_cfg)
    )

    # Ensure the modification time of the egg-info folder is updated so we
    # won't need to do this again.
    # TODO(stephenfin): Remove once the minimum version of setuptools is
    # high enough to include https://github.com/pypa/setuptools/pull/1427/
    if needs_reinstall:
        egg_info.setmtime()

    return needs_reinstall


# Traceback (pip._internal.exceptions.InstallationError)
GLOB sdist-make: /tmp/foo/setup.py
py36 inst-nodeps: /tmp/foo/.tox/dist/foo-0.0.0.zip
ERROR: invocation failed (exit code 1), logfile: /tmp/foo/.tox/py36/log/py36-3.log
ERROR: actionid: py36
msg: installpkg
cmdargs: ['/tmp/foo/.tox/py36/bin/pip', 'install', '-U', '--no-deps', '/tmp/foo/.tox/dist/foo-0.0.0.zip']

Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
Exception information:
Traceback (most recent call last):
File "/tmp/foo/.tox/py36/lib/python3.6/site-packages/pip/_internal/basecommand.py", line 228, in main
status = self.run(options, args)
File "/tmp/foo/.tox/py36/lib/python3.6/site-packages/pip/_internal/commands/install.py", line 218, in run
"Can not perform a '--user' install. User site-packages "
pip._internal.exceptions.InstallationError: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

py36 installed: foo==0.0.0
_____________________________________________________________________________________________ summary ______________________________________________________________________________________________
ERROR:   py36: InvocationError for command /tmp/foo/.tox/py36/bin/pip install -U --no-deps /tmp/foo/.tox/dist/foo-0.0.0.zip (see /tmp/foo/.tox/py36/log/py36-3.log) (exited with code 1)

# Fixed code
def _needs_reinstall(self, setupdir, action):
    setup_py = setupdir.join("setup.py")
    setup_cfg = setupdir.join("setup.cfg")
    args = [self.envconfig.envpython, str(setup_py), "--name"]
    env = self._get_os_environ()
    output = action.popen(args, cwd=setupdir, redirect=False, returnout=True, env=env)
    name = output.strip()
    args = [self.envconfig.envpython, "-c", "import sys; print(sys.path)"]
    out = action.popen(args, redirect=False, returnout=True, env=env)
    try:
        sys_path = ast.literal_eval(out.strip())
    except SyntaxError:
        sys_path = []
    egg_info_fname = ".".join((name, "egg-info"))
    for d in reversed(sys_path):
        egg_info = py.path.local(d).join(egg_info_fname)
        if egg_info.check():
            break
    else:
        return True
    needs_reinstall = any(
        conf_file.check() and conf_file.mtime() > egg_info.mtime()
        for conf_file in (setup_py, setup_cfg)
    )

    # Ensure the modification time of the egg-info folder is updated so we
    # won't need to do this again.
    # TODO(stephenfin): Remove once the minimum version of setuptools is
    # high enough to include https://github.com/pypa/setuptools/pull/1427/
    if needs_reinstall:
        egg_info.setmtime()

    return needs_reinstall