From 4e9c8f0dc5c318067c58abb0e197c779a3e08653 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Sun, 25 Nov 2018 16:33:53 +0800 Subject: [PATCH] Load .env AFTER environment activation This makes the environment's PATH and VIRTUAL_ENV variables available for use in the .env file. This is helpful for configuring tools like mypy that are not naturally aware of virtual environments. --- news/3299.behavior.rst | 1 + pipenv/core.py | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 news/3299.behavior.rst diff --git a/news/3299.behavior.rst b/news/3299.behavior.rst new file mode 100644 index 00000000..c727acac --- /dev/null +++ b/news/3299.behavior.rst @@ -0,0 +1 @@ +Virtual environment activation for ``pipenv run`` is now done *before* loading ``.env`` instead of after. This makes the activated environment variables available for ``.env`` configuration. diff --git a/pipenv/core.py b/pipenv/core.py index 035e4e7b..b81fea01 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -2298,6 +2298,21 @@ def do_run_posix(script, command): ) +def _setup_environment_for_child(): + # Set an environment variable, so we know we're in the environment. + os.environ["PIPENV_ACTIVE"] = vistir.misc.fs_str("1") + + # Unset so the child can refer to the correct pip. (sarugaku/pip-shims#23) + os.environ.pop("PIP_SHIMS_BASE_MODULE", None) + + # Activate virtualenv under the current interpreter's environment. + inline_activate_virtual_environment() + + # Load user-defined environment. This needs to be done after activation + # so the activated environment variables is available in the .env file. + load_dot_env() + + def do_run(command, args, three=None, python=False, pypi_mirror=None): """Attempt to run command either pulling from project or interpreting as executable. @@ -2310,14 +2325,8 @@ 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") + _setup_environment_for_child() - os.environ.pop("PIP_SHIMS_BASE_MODULE", None) - load_dot_env() - - # Activate virtualenv under the current interpreter's environment - inline_activate_virtual_environment() try: script = project.build_script(command, args) except ScriptEmptyError: