Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve inline virtualenv activation logic #2503

Merged
merged 2 commits into from
Jul 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,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 @@ -2145,12 +2145,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 @@ -2204,7 +2234,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