mirror of
https://github.com/kennethreitz/pipenv.git
synced 2026-06-05 22:50:18 +00:00
Actually load the dot env file before virtualenv creation.
This commit is contained in:
@@ -23,6 +23,7 @@ from pipenv.cli.options import (
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
|
||||
+2
-41
@@ -49,7 +49,7 @@ from pipenv.utils.shell import (
|
||||
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.
|
||||
|
||||
@@ -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,7 +3,8 @@ from tempfile import TemporaryDirectory
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user