Skip to content

Commit

Permalink
Scrub PYTHONPATH when creating venvs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Sep 16, 2021
1 parent f90db45 commit acc89ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 14 additions & 2 deletions pex/tools/commands/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,24 @@ def create(
)
interpreter = base_interpreter

# Guard against API calls from environment with ambient PYTHONPATH preventing pip virtualenv
# creation. See: https://github.com/pantsbuild/pex/issues/1451
env = os.environ.copy()
pythonpath = env.pop("PYTHONPATH", None)
if pythonpath:
TRACER.log(
"Scrubbed PYTHONPATH={} from the virtualenv creation environment.".format(
pythonpath
),
V=3,
)

if interpreter.version[0] >= 3 and not interpreter.identity.interpreter == "PyPy":
# N.B.: PyPy3 comes equipped with a venv module but it does not seem to work.
args = ["-m", "venv", "--without-pip", venv_dir]
if copies:
args.append("--copies")
interpreter.execute(args=args)
interpreter.execute(args=args, env=env)
else:
virtualenv_py = resource_string(__name__, "virtualenv_16.7.10_py")
with named_temporary_file(mode="wb") as fp:
Expand All @@ -103,7 +115,7 @@ def create(
args = [fp.name, "--no-pip", "--no-setuptools", "--no-wheel", venv_dir]
if copies:
args.append("--always-copy")
interpreter.execute(args=args)
interpreter.execute(args=args, env=env)
return cls(venv_dir)

@classmethod
Expand Down
6 changes: 2 additions & 4 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,8 @@ def test_issues_892():
python27 = PythonInterpreter.from_binary({python27!r})
result = resolver.resolve(requirements=['packaging==19.2'], interpreters=[python27])
print('Resolved: {{}}'.format(result))
""".format(
python27=python27
)
)
"""
).format(python27=python27)

python38 = ensure_python_interpreter(PY38)
cmd, process = PythonInterpreter.from_binary(python38).open_process(
Expand Down

0 comments on commit acc89ec

Please sign in to comment.