# Buggy code
def _spawn_pip_isolated(self, args, cache=None, interpreter=None):
    pip_args = ["--disable-pip-version-check", "--isolated", "--exists-action", "i"]

    # The max pip verbosity is -vvv and for pex it's -vvvvvvvvv; so we scale down by a factor of 3.
    pex_verbosity = ENV.PEX_VERBOSE
    pip_verbosity = pex_verbosity // 3
    if pip_verbosity > 0:
        pip_args.append("-{}".format("v" * pip_verbosity))
    else:
        pip_args.append("-q")

    if cache:
        pip_args.extend(["--cache-dir", cache])
    else:
        pip_args.append("--no-cache-dir")

    command = pip_args + args
    with ENV.strip().patch(
        PEX_ROOT=ENV.PEX_ROOT, PEX_VERBOSE=str(pex_verbosity)
    ) as env:
        from pex.pex import PEX

        pip = PEX(pex=self._pip_pex_path, interpreter=interpreter)
        return Job(
            command=pip.cmdline(command),
            process=pip.run(args=command, env=env, blocking=False),
        )


# Traceback (ModuleNotFoundError)
$ python3.8 <<EOF
import os
import sys


# This puts python3.8 stdlib on PYTHONPATH.
os.environ['PYTHONPATH'] = os.pathsep.join(sys.path)


from pex import resolver
from pex.interpreter import PythonInterpreter


def find_python36():
for interp in PythonInterpreter.iter():
if interp.version[:2] == (3, 6):
return interp
raise RuntimeError('This test requires python3.6 be on the PATH.')


result = resolver.resolve(requirements=['packaging==19.2'], interpreter=find_python36())
print('Resolved: {}'.format(result))
EOF
Fatal Python error: Py_Initialize: can't initialize sys standard streams
Traceback (most recent call last):
File "/usr/lib/python3.8/abc.py", line 64, in <module>
ModuleNotFoundError: No module named '_abc'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.8/io.py", line 52, in <module>
File "/usr/lib/python3.8/abc.py", line 68, in <module>
File "/usr/lib/python3.8/_py_abc.py", line 35
def __new__(mcls, name, bases, namespace, /, **kwargs):
^
SyntaxError: invalid syntax
Traceback (most recent call last):
File "<stdin>", line 20, in <module>
File "/home/jsirois/dev/pantsbuild/pex/pex/resolver.py", line 701, in resolve
return list(resolve_request.resolve_distributions(ignore_errors=ignore_errors))
File "/home/jsirois/dev/pantsbuild/pex/pex/resolver.py", line 498, in resolve_distributions
for resolve_result in self._run_parallel(inputs=to_resolve,
File "/home/jsirois/dev/pantsbuild/pex/pex/resolver.py", line 366, in _run_parallel
for result in execute_parallel(
File "/home/jsirois/dev/pantsbuild/pex/pex/jobs.py", line 219, in execute_parallel
raise error
pex.resolver.Unsatisfiable: pid: 13111 -> /usr/bin/python3.6 /tmp/tmp6ghsjpkj --disable-pip-version-check --isolated --exists-action i -q --no-cache-dir download --dest /tmp/tmpk4po5m_6/resolved_dists/cp36-cp36m packaging==19.2 raised Executing /usr/bin/python3.6 /tmp/tmp6ghsjpkj --disable-pip-version-check --isolated --exists-action i -q --no-cache-dir download --dest /tmp/tmpk4po5m_6/resolved_dists/cp36-cp36m packaging==19.2 failed with -6

# Fixed code
def _spawn_pip_isolated(self, args, cache=None, interpreter=None):
    pip_args = ["--disable-pip-version-check", "--isolated", "--exists-action", "i"]

    # The max pip verbosity is -vvv and for pex it's -vvvvvvvvv; so we scale down by a factor of 3.
    pex_verbosity = ENV.PEX_VERBOSE
    pip_verbosity = pex_verbosity // 3
    if pip_verbosity > 0:
        pip_args.append("-{}".format("v" * pip_verbosity))
    else:
        pip_args.append("-q")

    if cache:
        pip_args.extend(["--cache-dir", cache])
    else:
        pip_args.append("--no-cache-dir")

    command = pip_args + args
    with ENV.strip().patch(
        PEX_ROOT=ENV.PEX_ROOT, PEX_VERBOSE=str(pex_verbosity)
    ) as env:
        # Guard against API calls from environment with ambient PYTHONPATH preventing pip PEX
        # bootstrapping. See: https://github.com/pantsbuild/pex/issues/892
        pythonpath = env.pop("PYTHONPATH", None)
        if pythonpath:
            TRACER.log(
                "Scrubbed PYTHONPATH={} from the pip PEX environment.".format(
                    pythonpath
                ),
                V=3,
            )

        from pex.pex import PEX

        pip = PEX(pex=self._pip_pex_path, interpreter=interpreter)
        return Job(
            command=pip.cmdline(command),
            process=pip.run(args=command, env=env, blocking=False),
        )