From 8011c12dda4f916ef185f7d0dfc8a4db73a5b044 Mon Sep 17 00:00:00 2001 From: Al Johri Date: Mon, 11 Nov 2019 12:17:08 -0500 Subject: [PATCH] null out pythonpath (#252) * fixes #233 --- docs/changelog.md | 1 + src/pipx/main.py | 2 +- src/pipx/util.py | 3 ++- tests/test_run.py | 25 +++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index e8ca123e51..1ce6ec54fe 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,7 @@ dev - Handle missing interpreters more gracefully (#146) - Change `reinstall-all` to use system python by default for apps. Now use `--python` option to specify a different python version. +- Remove the PYTHONPATH environment variable when executing any command to prevent conflicts between pipx dependencies and package dependenies when pipx is installed via homebrew. Homebrew can use pythonpath manipulation instead of virtual environments. (#233) 0.14.0.0 diff --git a/src/pipx/main.py b/src/pipx/main.py index c6ac171f05..608b02c844 100644 --- a/src/pipx/main.py +++ b/src/pipx/main.py @@ -20,7 +20,7 @@ from .util import PipxError, mkdir from .venv import VenvContainer -__version__ = "0.14.0.0" +__version__ = "0.14.0.1" def simple_parse_version(s, segments=4) -> Tuple[Union[int, str], ...]: diff --git a/src/pipx/util.py b/src/pipx/util.py index 05bafc1eaa..21a25e2cc6 100644 --- a/src/pipx/util.py +++ b/src/pipx/util.py @@ -94,11 +94,12 @@ def get_site_packages(python: Path) -> Path: def run(cmd: Sequence[Union[str, Path]], check=True) -> int: """Run arbitrary command as subprocess""" + env = {k: v for k, v in os.environ.items() if k.upper() != "PYTHONPATH"} cmd_str = " ".join(str(c) for c in cmd) logging.info(f"running {cmd_str}") # windows cannot take Path objects, only strings cmd_str_list = [str(c) for c in cmd] - returncode = subprocess.run(cmd_str_list).returncode + returncode = subprocess.run(cmd_str_list, env=env).returncode if check and returncode: raise PipxError(f"{cmd_str!r} failed") return returncode diff --git a/tests/test_run.py b/tests/test_run.py index 1ee73968af..995bf09897 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 +import os import logging import sys +import subprocess from unittest import mock import pytest # type: ignore @@ -42,3 +44,26 @@ def test_run_script_from_internet(pipx_temp_env, capsys): "6bdfbb6e9c1132b1c38fdd2f195d4a24c540c324/pipx-demo.py", ] ) + + +def test_run_ensure_null_pythonpath(): + env = os.environ.copy() + env["PYTHONPATH"] = "test" + assert ( + "None" + in subprocess.run( + [ + sys.executable, + "-m", + "pipx", + "run", + "ipython", + "-c", + "import os; print(os.environ.get('PYTHONPATH'))", + ], + universal_newlines=True, + env=env, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ).stdout + )