From 76ca3e3ba19b51899c68bccf9b8dcd376ec15720 Mon Sep 17 00:00:00 2001 From: jxltom Date: Sat, 1 Dec 2018 20:58:30 +0800 Subject: [PATCH 1/5] Set PIPENV_ACTIVATE after virtualenv_location is being used --- pipenv/core.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index 121adb57b8..be83af7a3e 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -2159,11 +2159,6 @@ def do_shell(three=None, python=False, fancy=False, shell_args=None, pypi_mirror three=three, python=python, validate=False, pypi_mirror=pypi_mirror, ) - # Set an environment variable, so we know we're in the environment. - os.environ["PIPENV_ACTIVE"] = vistir.misc.fs_str("1") - - os.environ.pop("PIP_SHIMS_BASE_MODULE", None) - # Support shell compatibility mode. if PIPENV_SHELL_FANCY: fancy = True @@ -2179,6 +2174,12 @@ def do_shell(three=None, python=False, fancy=False, shell_args=None, pypi_mirror shell_args, ) + # Set an environment variable, so we know we're in the environment. + # Only set PIPENV_ACTIVE after finishing reading virtualenv_location + # otherwise its value will be changed + os.environ["PIPENV_ACTIVE"] = vistir.misc.fs_str("1") + os.environ.pop("PIP_SHIMS_BASE_MODULE", None) + if fancy: shell.fork(*fork_args) return From 5e29e7635ee583456de1d453bee49fca9becd0e3 Mon Sep 17 00:00:00 2001 From: jxltom Date: Sat, 1 Dec 2018 21:01:25 +0800 Subject: [PATCH 2/5] Add bugfix news for 3339 --- news/3339.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/3339.bugfix diff --git a/news/3339.bugfix b/news/3339.bugfix new file mode 100644 index 0000000000..8e67e36f96 --- /dev/null +++ b/news/3339.bugfix @@ -0,0 +1 @@ +Fix a bug where custom virtualenv can not be activated with pipenv shell From d165d656365e3edd63833e7b8d3c2ea2d3aafe6f Mon Sep 17 00:00:00 2001 From: jxltom Date: Sat, 1 Dec 2018 22:23:27 +0800 Subject: [PATCH 3/5] Add blank line since these two environment variable has different purpose --- pipenv/core.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pipenv/core.py b/pipenv/core.py index be83af7a3e..cb7ce9e2f8 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -2178,6 +2178,7 @@ def do_shell(three=None, python=False, fancy=False, shell_args=None, pypi_mirror # Only set PIPENV_ACTIVE after finishing reading virtualenv_location # otherwise its value will be changed os.environ["PIPENV_ACTIVE"] = vistir.misc.fs_str("1") + os.environ.pop("PIP_SHIMS_BASE_MODULE", None) if fancy: From dbb3603a05a35277a44f1317ba717f1780bf05e9 Mon Sep 17 00:00:00 2001 From: jxltom Date: Mon, 3 Dec 2018 09:27:23 +0800 Subject: [PATCH 4/5] Fix a bug where activated virtualenv is not being used in pipenv run --- pipenv/core.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index cb7ce9e2f8..818d0beba6 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -2315,14 +2315,19 @@ def do_run(command, args, three=None, python=False, pypi_mirror=None): three=three, python=python, validate=False, pypi_mirror=pypi_mirror, ) - # Set an environment variable, so we know we're in the environment. - os.environ["PIPENV_ACTIVE"] = vistir.misc.fs_str("1") - - os.environ.pop("PIP_SHIMS_BASE_MODULE", None) load_dot_env() # Activate virtualenv under the current interpreter's environment inline_activate_virtual_environment() + + # Set an environment variable, so we know we're in the environment. + # Only set PIPENV_ACTIVE after finishing reading virtualenv_location + # such as in inline_activate_virtual_environment + # otherwise its value will be changed + os.environ["PIPENV_ACTIVE"] = vistir.misc.fs_str("1") + + os.environ.pop("PIP_SHIMS_BASE_MODULE", None) + try: script = project.build_script(command, args) except ScriptEmptyError: From 16db36e60fd759d35cb99c779354124d13eea20e Mon Sep 17 00:00:00 2001 From: jxltom Date: Sun, 16 Dec 2018 19:46:54 +0800 Subject: [PATCH 5/5] Test for pipenv behavior in activated virtualenv --- tests/integration/test_project.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/integration/test_project.py b/tests/integration/test_project.py index 6a4a858ae4..427545219a 100644 --- a/tests/integration/test_project.py +++ b/tests/integration/test_project.py @@ -170,7 +170,15 @@ def test_include_editable_packages(PipenvInstance, pypi, testsroot, pathlib_tmpd @pytest.mark.virtualenv def test_run_in_virtualenv(PipenvInstance, pypi, virtualenv): with PipenvInstance(chdir=True, pypi=pypi) as p: + os.environ['PIPENV_IGNORE_VIRTUALENVS'] = '1' + c = p.pipenv('run which pip') + assert c.return_code == 0 + assert 'virtualenv' not in c.out + os.environ.pop("PIPENV_IGNORE_VIRTUALENVS", None) + c = p.pipenv('run which pip') + assert c.return_code == 0 + assert 'virtualenv' in c.out project = Project() assert project.virtualenv_location == str(virtualenv) c = p.pipenv("run pip install click")