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

Actually load the dot env file before virtualenv creation. #5338

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions pipenv/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -421,7 +424,6 @@ def run(state, command, args):
three=state.three,
python=state.python,
pypi_mirror=state.pypi_mirror,
quiet=state.quiet,
)


Expand Down
43 changes: 2 additions & 41 deletions pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
41 changes: 41 additions & 0 deletions pipenv/utils/environment.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 2 additions & 1 deletion tests/unit/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down