Skip to content

Commit

Permalink
Merge pull request #3340 from jxltom/activate-custom-virtualenv
Browse files Browse the repository at this point in the history
Fix bug where custom virtualenv can not be activated by pipenv shell
  • Loading branch information
jxltom authored Jan 18, 2019
2 parents 1f36ca1 + afe1f4d commit 44db5dd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions news/3339.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where custom virtualenv can not be activated with pipenv shell
25 changes: 16 additions & 9 deletions pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2139,11 +2139,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
Expand All @@ -2159,6 +2154,13 @@ 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
Expand Down Expand Up @@ -2293,14 +2295,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)
cmd_string = ' '.join([script.command] + script.args)
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 44db5dd

Please sign in to comment.