From 51a82b4caede90753ef78bc4d33290a02b116eda Mon Sep 17 00:00:00 2001 From: Dan Ryan Date: Fri, 13 Oct 2017 18:33:14 -0400 Subject: [PATCH] Fix import failures using pew and psutil * Borrow the temp_environ context manager from pew * This avoids importing it which breaks on windows --- pipenv/cli.py | 6 ++---- pipenv/utils.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pipenv/cli.py b/pipenv/cli.py index 99a2456d..154fd593 100644 --- a/pipenv/cli.py +++ b/pipenv/cli.py @@ -25,19 +25,17 @@ import pipdeptree import requirements import semver import flake8.main.cli -import pew from pipreqs import pipreqs from blindspin import spinner from urllib3.exceptions import InsecureRequestWarning from pip.req.req_file import parse_requirements from click_didyoumean import DYMCommandCollection - from .project import Project from .utils import ( convert_deps_from_pip, convert_deps_to_pip, is_required_version, proper_case, pep423_name, split_vcs, resolve_deps, shellquote, is_vcs, python_version, suggest_package, find_windows_executable, is_file, - prepare_pip_source_args + prepare_pip_source_args, temp_environ ) from .__version__ import __version__ from . import pep508checker, progress @@ -2039,7 +2037,7 @@ def do_shell(three=None, python=False, fancy=False, shell_args=None): except AttributeError: import subprocess # Tell pew to use the project directory as its workon_home - with pew.pew.temp_environ(): + with temp_environ(): os.environ['WORKON_HOME'] = project.project_directory p = subprocess.Popen([cmd] + list(args), shell=True, universal_newlines=True) p.communicate() diff --git a/pipenv/utils.py b/pipenv/utils.py index 3fea5fab..3008a728 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -22,6 +22,7 @@ try: except ImportError: from urlparse import urlparse +from contextlib import contextmanager from piptools.resolver import Resolver from piptools.repositories.pypi import PyPIRepository from piptools.scripts.compile import get_pip_command @@ -913,3 +914,16 @@ def find_requirements(max_depth=3): if os.path.isfile(r): return r raise RuntimeError('No requirements.txt found!') + + +# Borrowed from pew to avoid importing pew which imports psutil +# See https://github.com/berdario/pew/blob/master/pew/_utils.py#L82 +@contextmanager +def temp_environ(): + """Allow the ability to set os.environ temporarily""" + environ = dict(os.environ) + try: + yield + finally: + os.environ.clear() + os.environ.update(environ)