From 3fa64125c270b5abddad337657397d89a8e2c757 Mon Sep 17 00:00:00 2001 From: Dan Ryan Date: Thu, 12 Apr 2018 18:43:05 -0400 Subject: [PATCH] Fix `create_pipfile` version parsing Signed-off-by: Dan Ryan --- pipenv/project.py | 18 ++++++++++++------ pipenv/utils.py | 9 +++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pipenv/project.py b/pipenv/project.py index 46bf8103..642dc519 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -44,6 +44,7 @@ from .environments import ( PIPENV_VIRTUALENV, PIPENV_TEST_INDEX, PIPENV_PYTHON, + PIPENV_DEFAULT_PYTHON_VERSION, ) @@ -578,12 +579,17 @@ class Project(object): u'dev-packages': {}, } # Default requires. - required_python = python or self.which( - 'python', self.virtualenv_location - ) - data[u'requires'] = { - 'python_version': python_version(required_python)[: len('2.7')] - } + required_python = python + if not python: + if self.virtualenv_location: + required_python = self.which('python', self.virtualenv_location) + else: + required_python = self.which('python') + version = python_version(required_python) or PIPENV_DEFAULT_PYTHON_VERSION + if version and len(version) >= 3: + data[u'requires'] = { + 'python_version': version[: len('2.7')] + } self.write_toml(data, 'Pipfile') def write_toml(self, data, path=None): diff --git a/pipenv/utils.py b/pipenv/utils.py index e3eba96e..4a4ce008 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -220,12 +220,13 @@ def python_version(path_to_python): c = delegator.run([path_to_python, '--version'], block=False) except Exception: return None - if c.return_code != 0: - return None + c.block() version = parse_python_version(c.out.strip() or c.err.strip()) - if not version: + try: + version = u'{major}.{minor}.{micro}'.format(**version) + except TypeError: return None - return u'{major}.{minor}.{micro}'.format(**version) + return version def escape_grouped_arguments(s):