diff --git a/pipenv/utils.py b/pipenv/utils.py index a19f7ed0f6..8188ee4316 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -180,12 +180,18 @@ def __init__(self, python_version, python_path): self.python_path = python_path def __enter__(self): - os.environ['PIP_PYTHON_VERSION'] = str(self.python_version) - os.environ['PIP_PYTHON_PATH'] = str(self.python_path) + # Only inject when the value is valid + if self.python_version: + os.environ['PIP_PYTHON_VERSION'] = str(self.python_version) + if self.python_path: + os.environ['PIP_PYTHON_PATH'] = str(self.python_path) def __exit__(self, *args): # Restore original Python version information. - del os.environ['PIP_PYTHON_VERSION'] + try: + del os.environ['PIP_PYTHON_VERSION'] + except KeyError: + pass def prepare_pip_source_args(sources, pip_args=None): diff --git a/tests/integration/test_lock.py b/tests/integration/test_lock.py index 0dfd22bb41..705e62a6d0 100644 --- a/tests/integration/test_lock.py +++ b/tests/integration/test_lock.py @@ -1,5 +1,6 @@ import pytest import os +import six from pipenv.utils import temp_environ @@ -331,7 +332,7 @@ def test_lock_updated_source(PipenvInstance, pypi): @pytest.mark.lock @pytest.mark.vcs @pytest.mark.needs_internet -def lock_editable_vcs_without_install(PipenvInstance, pypi): +def test_lock_editable_vcs_without_install(PipenvInstance, pypi): with PipenvInstance(pypi=pypi, chdir=True) as p: with open(p.pipfile_path, 'w') as f: f.write(""" @@ -345,3 +346,17 @@ def lock_editable_vcs_without_install(PipenvInstance, pypi): assert 'chardet' in p.lockfile['default'] c = p.pipenv('install') assert c.return_code == 0 + + +@pytest.mark.lock +def test_lock_respecting_python_version(PipenvInstance, pypi): + with PipenvInstance(pypi=pypi) as p: + with open(p.pipfile_path, 'w') as f: + f.write(""" +[packages] +django = "*" + """.strip()) + django_version = '==2.0.6' if six.PY3 else '==1.11.10' + c = p.pipenv('lock') + assert c.return_code == 0 + assert p.lockfile['default']['django']['version'] == django_version diff --git a/tests/pypi/django/Django-2.0.6.tar.gz b/tests/pypi/django/Django-2.0.6.tar.gz new file mode 100644 index 0000000000..bc69557b4f Binary files /dev/null and b/tests/pypi/django/Django-2.0.6.tar.gz differ