Skip to content

Commit

Permalink
Improve inline virtualenv activation logic
Browse files Browse the repository at this point in the history
Set VIRTUAL_ENV during activation so py.exe does the right thing. Also add
venv support --- it is actually quite easy, so why not.
  • Loading branch information
uranusjr committed Jul 3, 2018
1 parent 7daedb0 commit eb953fe
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2135,7 +2135,7 @@ def do_shell(three=None, python=False, fancy=False, shell_args=None, pypi_mirror
shell.fork(*fork_args)


def inline_activate_virtualenv():
def _inline_activate_virtualenv():
try:
activate_this = which("activate_this.py")
if not activate_this or not os.path.exists(activate_this):
Expand All @@ -2152,12 +2152,42 @@ def inline_activate_virtualenv():
# Catch all errors, just in case.
except Exception:
click.echo(
u"{0}: There was an unexpected error while activating your virtualenv. Continuing anyway..."
"".format(crayons.red("Warning", bold=True)),
u"{0}: There was an unexpected error while activating your "
u"virtualenv. Continuing anyway...".format(
crayons.red("Warning", bold=True),
),
err=True,
)


def _inline_activate_venv():
"""Built-in venv doesn't have activate_this.py, but doesn't need it anyway.
As long as we find the correct executable, built-in venv sets up the
environment automatically.
See: https://bugs.python.org/issue21496#msg218455
"""
components = []
for name in ('bin', 'Scripts'):
bindir = os.path.join(project.virtualenv_location, name)
if os.path.exists(bindir):
components.append(bindir)
if 'PATH' in os.environ:
components.append(os.environ['PATH'])
os.environ['PATH'] = os.pathsep.join(components)


def inline_activate_virtual_environment():
root = project.virtualenv_location
if os.path.exists(os.path.join(root, 'pyvenv.cfg')):
_inline_activate_venv()
else:
_inline_activate_virtualenv()
if 'VIRTUAL_ENV' not in os.environ:
os.environ['VIRTUAL_ENV'] = root


def do_run_nt(script):
import subprocess

Expand Down Expand Up @@ -2211,7 +2241,7 @@ def do_run(command, args, three=None, python=False, pypi_mirror=None):
ensure_project(three=three, python=python, validate=False, pypi_mirror=pypi_mirror)
load_dot_env()
# Activate virtualenv under the current interpreter's environment
inline_activate_virtualenv()
inline_activate_virtual_environment()
try:
script = project.build_script(command, args)
except ScriptEmptyError:
Expand Down

0 comments on commit eb953fe

Please sign in to comment.