From 5e7aee3a77cbc4647f13afeaa89ec0d22b767e9e Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 7 Sep 2022 19:23:41 -0400 Subject: [PATCH] Actually load the dot env file before virtualenv creation. --- pipenv/cli/command.py | 6 ++++-- pipenv/core.py | 43 ++----------------------------------- pipenv/utils/environment.py | 41 +++++++++++++++++++++++++++++++++++ tests/unit/test_core.py | 3 ++- 4 files changed, 49 insertions(+), 44 deletions(-) create mode 100644 pipenv/utils/environment.py diff --git a/pipenv/cli/command.py b/pipenv/cli/command.py index 1faf6c6cb3..0a8abd24ef 100644 --- a/pipenv/cli/command.py +++ b/pipenv/cli/command.py @@ -23,6 +23,7 @@ uninstall_options, verbose_option, ) +from pipenv.utils.environment import load_dot_env from pipenv.utils.processes import subprocess_run from pipenv.vendor.click import ( Choice, @@ -82,6 +83,8 @@ def cli( from pipenv.utils.shell import system_which from pipenv.utils.spinner import create_spinner + load_dot_env(state.project, quiet=state.quiet) + from ..core import ( cleanup_virtualenv, do_clear, @@ -371,7 +374,7 @@ def shell( anyway=False, ): """Spawns a shell within the virtualenv.""" - from ..core import do_shell, load_dot_env + from ..core import do_shell # Prevent user from activating nested environments. if "PIPENV_ACTIVE" in os.environ: @@ -421,7 +424,6 @@ def run(state, command, args): three=state.three, python=state.python, pypi_mirror=state.pypi_mirror, - quiet=state.quiet, ) diff --git a/pipenv/core.py b/pipenv/core.py index f6d012b620..a22061fec2 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -49,7 +49,7 @@ system_which, ) from pipenv.utils.spinner import create_spinner -from pipenv.vendor import click, dotenv, vistir +from pipenv.vendor import click, vistir from pipenv.vendor.requirementslib.models.requirements import Requirement if MYPY_RUNNING: @@ -113,42 +113,6 @@ def do_clear(project): raise -def load_dot_env(project, as_dict=False, quiet=False): - """Loads .env file into sys.environ.""" - if not project.s.PIPENV_DONT_LOAD_ENV: - # If the project doesn't exist yet, check current directory for a .env file - project_directory = project.project_directory or "." - dotenv_file = project.s.PIPENV_DOTENV_LOCATION or os.sep.join( - [project_directory, ".env"] - ) - - if not os.path.isfile(dotenv_file) and project.s.PIPENV_DOTENV_LOCATION: - click.echo( - "{}: file {}={} does not exist!!\n{}".format( - click.style("Warning", fg="red", bold=True), - click.style("PIPENV_DOTENV_LOCATION", bold=True), - click.style(project.s.PIPENV_DOTENV_LOCATION, bold=True), - click.style( - "Not loading environment variables.", fg="red", bold=True - ), - ), - err=True, - ) - if as_dict: - return dotenv.dotenv_values(dotenv_file) - elif os.path.isfile(dotenv_file): - if not quiet: - click.echo( - click.style( - fix_utf8("Loading .env environment variables..."), bold=True - ), - err=True, - ) - dotenv.load_dotenv(dotenv_file, override=True) - - project.s = environments.Setting() - - def cleanup_virtualenv(project, bare=True): """Removes the virtualenv directory from the system.""" if not bare: @@ -2722,16 +2686,13 @@ def do_run_posix(project, script, command, env): ) -def do_run( - project, command, args, three=None, python=False, pypi_mirror=None, quiet=False -): +def do_run(project, command, args, three=None, python=False, pypi_mirror=None): """Attempt to run command either pulling from project or interpreting as executable. Args are appended to the command in [scripts] section of project if found. """ from .cmdparse import ScriptEmptyError - load_dot_env(project, quiet=quiet) env = os.environ.copy() # Ensure that virtualenv is available. diff --git a/pipenv/utils/environment.py b/pipenv/utils/environment.py new file mode 100644 index 0000000000..abcbe4a559 --- /dev/null +++ b/pipenv/utils/environment.py @@ -0,0 +1,41 @@ +import os + +from pipenv import environments +from pipenv._compat import fix_utf8 +from pipenv.vendor import click, dotenv + + +def load_dot_env(project, as_dict=False, quiet=False): + """Loads .env file into sys.environ.""" + if not project.s.PIPENV_DONT_LOAD_ENV: + # If the project doesn't exist yet, check current directory for a .env file + project_directory = project.project_directory or "." + dotenv_file = project.s.PIPENV_DOTENV_LOCATION or os.sep.join( + [project_directory, ".env"] + ) + + if not os.path.isfile(dotenv_file) and project.s.PIPENV_DOTENV_LOCATION: + click.echo( + "{}: file {}={} does not exist!!\n{}".format( + click.style("Warning", fg="red", bold=True), + click.style("PIPENV_DOTENV_LOCATION", bold=True), + click.style(project.s.PIPENV_DOTENV_LOCATION, bold=True), + click.style( + "Not loading environment variables.", fg="red", bold=True + ), + ), + err=True, + ) + if as_dict: + return dotenv.dotenv_values(dotenv_file) + elif os.path.isfile(dotenv_file): + if not quiet: + click.echo( + click.style( + fix_utf8("Loading .env environment variables..."), bold=True + ), + err=True, + ) + dotenv.load_dotenv(dotenv_file, override=True) + + project.s = environments.Setting() diff --git a/tests/unit/test_core.py b/tests/unit/test_core.py index ac5de01012..adcc3e19d1 100644 --- a/tests/unit/test_core.py +++ b/tests/unit/test_core.py @@ -3,7 +3,8 @@ import pytest -from pipenv.core import load_dot_env, warn_in_virtualenv +from pipenv.core import warn_in_virtualenv +from pipenv.utils.environment import load_dot_env from pipenv.utils.shell import temp_environ